我的问题是 ng-if 的行为并不像我假装的那样,我有一个带有 _name 属性的对象数组,我做了一个 ng-repeat ,在里面我想区分一些以特定方式命名的对象和其他的。基于我正在制作我的 ng-if,但是打印了两次行,并且它的内容应该是其中之一,任何人都可以指出我失败的地方吗?谢了
在 clusterEntity.cluster 中的数组下方;
locations:{
location:[
{_name: "staging", _path: ""},
{_name: "temp", _path: ""},
{_name: "working", _path: ""}
]
},
<div ng-repeat="location in clusterEntity.cluster.locations.location">
<div ng-if="
location._name === 'staging' ||
location._name === 'working' ||
location._name === 'temp'">
<something here>
</div>
<div ng-if="
location._name !== 'staging' ||
location._name !== 'working' ||
location._name !== 'temp'">
<something there>
</div>
</div>
最佳答案
你的第二个ng-if
应该使用逻辑AND,而不是OR:
<div class="row" ng-if="location._name !== 'staging' && location._name !== 'working' && location._name !== 'temp'">
这与:
相同<div class="row" ng-if="!(location._name === 'staging' || location._name === 'working' || location._name === 'temp')">
您可以使用ng-switch
相反,但您的代码不会短得多。
关于javascript - ng-如果 Angular 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26786616/