如何在ng-repeat over array中使用ng-if?

时间:2017-04-04 17:44:06

标签: javascript angularjs angularjs-ng-repeat

我有一个或多个颜色= ['红色','白色','金色','黑色']; 我试图做下面的代码......

<span ng-repeat="color in item.colorOptions" ng-init="color">
    <lable ng-if="'Red' == {{color}}"> This is Red..</lable>
    <lable ng-if="'Black' == {{color}}"> This is Black..</lable>
</span>

为什么我不能使用ng-if如上面的代码?什么是原因以及可以在这里使用ng-if或ng-show / hide的替代方法?

1 个答案:

答案 0 :(得分:1)

应该是,

<span ng-repeat="color in item.colorOptions" ng-init="color">
    <lable ng-if="color === 'Red'"> This is Red..</lable>
    <lable ng-if="color === 'Black'"> This is Black..</lable>
</span>

<强>样本

var myApp=angular.module('myApp',[])
 myApp.controller('myController',function($scope){
  $scope.item = {};
  $scope.item.colorOptions = ['Red', 'White', 'Gold', 'Black'];
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<div ng-app='myApp' ng-controller='myController'>
    <span ng-repeat="color in item.colorOptions" ng-init="color">
    <lable ng-if="color === 'Red'"> This is Red..</lable>
    <lable ng-if="color === 'Black'"> This is Black..</lable>
</span> 
</div>