2017-04-04 77 views
1

我有一个数组或颜色= ['红','白','金','黑']; 我试图做类似下面的代码..如何使用ng-if在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-如果像上面的代码?什么是一个原因,什么可能是另一种使用ng-if或ng-show/hide在这里的方法?

+0

这里假设item.colorOptions = [ '红', '白', '黄金','黑色']; – Laxmikant

回答

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> 

DEMO

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>

相关问题