0

What I need to do is show button in table, but when "name" is "True" the button should not show. Only when "name" is "False: the button should be in the table.

My Json

[
        "name" : "False",
        "date" : "22/02/2015"
    },
    {
        "name" : "False",
        "date" : "18/03/2013"
    },
    {
        "name" : "True",
        "date" : "12/06/2012"
    }]

My table

<tr ng-repeat="name in names">
   <td>{{name.name}}</td>
   <td>{{name.date}}</td>
   <td><button ng-model="post">POST</button></td>
</tr>
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
bafix2203
  • 541
  • 7
  • 25

4 Answers4

0

You could use ng-if to show and hide button

<tr ng-repeat="name in names">
    <td>{{name.name}}</td>
    <td>{{name.date}}</td>
    <td>
        <button ng-if="name.name === 'False'">POST</button>
    </td>
</tr>
Pankaj Parkar
  • 134,766
  • 23
  • 234
  • 299
0

you can use ng-if or ng-show or ng-hide

<tr ng-repeat="name in names">
<td>{{name.name}}</td>
<td>{{name.date}}</td>
<td>
    <button ng-if="!name.name">POST</button>
</td>

jitendra varshney
  • 3,484
  • 1
  • 21
  • 31
0

Go througth this link https://stackoverflow.com/a/21870119/6554634 to get better idea about ng-if, ng-show and ng-hide.

And there is mistake in your data, there is no opening brace for first object. This is silly but causes loss if we didn't notice.

[
    {
        "name" : "False",
        "date" : "22/02/2015"
    },
    {
        "name" : "False",
        "date" : "18/03/2013"
    },
    {
        "name" : "True",
        "date" : "12/06/2012"
    }
]

I prefer ng-if as it removes element from DOM if not necessary.

<tr ng-repeat="name in names">
   <td>{{name.name}}</td>
   <td>{{name.date}}</td>
   <td><button ng-model="post" ng-if="name.name==='False'">POST</button></td>
</tr>
Community
  • 1
  • 1
Mr_Perfect
  • 8,254
  • 11
  • 35
  • 62
0
  • Your JSON is invalid
  • Use ng-show to show the button when name is True.

Working demo :

var myApp = angular.module('myApp',[]);

myApp.controller('MyCtrl', function ($scope) {
    $scope.names = [{
        "name" : "False",
        "date" : "22/02/2015"
    },
    {
        "name" : "False",
        "date" : "18/03/2013"
    },
    {
        "name" : "True",
        "date" : "12/06/2012"
    }];
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="MyCtrl">
<table>
<tr ng-repeat="name in names">
   <td>{{name.name}}</td>
   <td>{{name.date}}</td>
   <td><button ng-model="post" ng-show="name.name == 'True'">POST</button></td>
</tr>
</table>
</div>
Debug Diva
  • 26,058
  • 13
  • 70
  • 123