gpt4 book ai didi

javascript - 如何在数组上的 ng-repeat 内部使用 ng-if ?

转载 作者:太空宇宙 更新时间:2023-11-04 15:50:01 25 4
gpt4 key购买 nike

我有一个数组或颜色 = ['Red', 'White', 'Gold', 'Black'];我试图做类似下面的代码的事情..

<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 的替代方法是什么?

最佳答案

应该是

<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>

关于javascript - 如何在数组上的 ng-repeat 内部使用 ng-if ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43214138/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com