我正在研究AngularJS Web应用程序,有时我需要检查ng-if或ng-show指令中的元素是否在列表中。我现在正在这样做:
<div ng-if="object.element=='A' || object.element=='B' || object.element=='C'">
<p>Hello World!</p>
</div>
我想知道是否有办法做这样的事情:
<div ng-if="object.element in ['A','B','C']">
<p>Hello World!</p>
</div>
答案 0 :(得分:2)
你可以这样做:
<div ng-if="['A','B','C'].indexOf(object.element)>-1">
<p>Hello World!</p>
</div>
或者这个(它是一样的):
<div ng-if="['A','B','C'].indexOf(object.element)+1">
<p>Hello World!</p>
</div>
答案 1 :(得分:2)
试试这个
<强> HTML 强>
<div ng-if="HideShow()">
<p>Hello World!</p>
</div>
<强> JS 强>
$scope.object = {
element: 'A'
};
$scope.HideShow = function () {
return $scope.object.element == 'A' || $scope.object.element == 'B' || $scope.object.element == 'C'
}