2017-04-04 63 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>

相關問題