javascript - Angular ng-repeat not show empty object attributes -
i trying loop through using data below in angular ng-repeat
{ "qid": "173995x306x4091", "gid": null, "comments": "milestone1: here milestone1details", "owner": "me", "targetdate": "28-10-2016" }, { "qid": "173995x306x4101", "gid": null, "comments": "", "owner": "", "targetdate": "" }
html:
<div class="modal-body" ng-repeat="milestone in milestones "> <table class="table table-striped"> <thead> <tr> <th>milestone </th> <th>milestone owner</th> <th>milestone target date</th> </tr> </thead> <tr> <td>{{milestone.comments }} </td> <td>{{milestone.owner }}</td> <td>{{milestone.targetdate }}</td> </tr> </table> </div>
i don't want empty attributes show : below second object
<table class="table table-striped"> <thead> <tr> <th>milestone </th> <th>milestone owner</th> <th>milestone target date</th> </tr> </thead> <tbody><tr> <td class="ng-binding"> </td> <td class="ng-binding"></td> <td class="ng-binding"></td> </tr> </tbody> </table>
how can this?
just add condition in tr
following:
<tr ng-if="milestone.comments && milestone.owner && milestone.targetdate">
alternatively, can use ng-show
instead of ng-if
. both not display row difference ng-if
remove empty row dom while ng-show
hide row using css class.
edit: also, suggest moving ng-repeat
condition tr
(if not have specific requirement). see working example below:
var app = angular.module("sa", []); app.controller("foocontroller", function($scope) { $scope.milestones = [{ "qid": "173995x306x4091", "gid": null, "comments": "milestone1: here milestone1details", "owner": "me", "targetdate": "28-10-2016" }, { "qid": "173995x306x4101", "gid": null, "comments": "", "owner": "", "targetdate": "" }]; });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mtjoasx8j1au+a5wdvnpi2lkffwweaa8hdddjzlplegxhjvme1fgjwpgmkzs7" crossorigin="anonymous"> <div ng-app="sa" ng-controller="foocontroller" class="container"> <table class="table table-striped table-bordered"> <thead> <tr> <th>milestone</th> <th>milestone owner</th> <th>milestone target date</th> </tr> </thead> <tbody> <tr ng-repeat="milestone in milestones" ng-if="milestone.comments && milestone.owner && milestone.targetdate"> <td>{{milestone.comments }}</td> <td>{{milestone.owner }}</td> <td>{{milestone.targetdate }}</td> </tr> </tbody> </table> </div>
like @ved commented/answered, if there space between quotes, can modify query this:
<tr ng-if="milestone.comments.trim() && milestone.owner.trim() && milestone.targetdate.trim()">
there not error if of value undefined/null.
Comments
Post a Comment