我想使用ng-if

时间:2015-06-26 20:55:33

标签: javascript angularjs angularjs-ng-repeat accordion angular-ng-if

我正在尝试在ng-repeat中使用ng-if来实现Accordions。根据条件值,ng-repeat应跳过ng-repeat中的一些项目。

E.g。如果item.condition为true,则只显示accordion。 下面的代码是我到目前为止,它没有工作。看起来不错吗?

     <accordion close-others="true">
          <accordion-group is-open="isopen" ng-repeat="item in items | limitTo:2" ng-if="item.condition == "true"",ng-init="isopen=2">
                  <accordion-heading>
          {{item.label}}
            <i class="pull-right glyphicon"
                         ng-class="{'icon-arrow-up': isopen, 'icon-arrow-down': !isopen}"></i>
                  </accordion-heading>
              </accordion-group>
          </accordion>

1 个答案:

答案 0 :(得分:5)

您的ng-if包含双倍额外引号,应为ng-if="item.condition == true",同时从手风琴元素中移除,

此外,您可以将条件最小化为ng-if="item.condition",然后表达式将在true变量评估中返回falseitem.condition

<强>标记

<accordion close-others="true">
    <accordion-group is-open="isopen" ng-repeat="item in items | limitTo:2" 
       ng-if="item.condition" ng-init="isopen=2">
        <accordion-heading>
            {{item.label}}
            <i class="pull-right glyphicon" ng-class="{'icon-arrow-up': isopen, 'icon-arrow-down': !isopen}"></i>
        </accordion-heading>
    </accordion-group>
</accordion>