在AngularJS中迭代ng-if或ng-show

时间:2014-09-19 03:33:04

标签: javascript angularjs angularjs-directive

我正在研究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>

2 个答案:

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

Working example

答案 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'
    }