你可以一起使用ng-switch和ng-if吗?

时间:2014-05-16 22:28:05

标签: javascript html eclipse angularjs angularjs-directive

我试图同时使用角度ng-switchng-if指令,但它似乎并没有起作用。我在做的是:

<div data-ng-if = "x === 'someValue'" data-ng-switch on = "booleanValue">
   <div data-ng-switch-when = "!booleanValue" data-ng-include="'partial/index.html'"></div>
   <div data-ng-switch-when = "booleanValue" data-ng-include="'partial/pageTwo.html'"></div>
</div>

我只是想知道这是否可行,或者是否有另一种方法可以根据$ scope中的某些条件切换这些部分?

1 个答案:

答案 0 :(得分:1)

在您的ng-switch-when中,使用truefalse代替booleanValue!booleanValue

<div data-ng-if = "x === 'someValue'" data-ng-switch on = "booleanValue">
   <div data-ng-switch-when = "false" data-ng-include="'partial/index.html'"></div>
   <div data-ng-switch-when = "true" data-ng-include="'partial/pageTwo.html'"></div>
</div>

您也可以使用ng-show:

<div data-ng-if = "x === 'someValue'" >
   <div data-ng-show = "!booleanValue" data-ng-include="'partial/index.html'"></div>
   <div data-ng-show = "booleanValue" data-ng-include="'partial/pageTwo.html'"></div>
</div>

或者再次使用ng-if:

<div data-ng-if = "x === 'someValue'" >
   <div data-ng-if = "!booleanValue" data-ng-include="'partial/index.html'"></div>
   <div data-ng-if = "booleanValue" data-ng-include="'partial/pageTwo.html'"></div>
</div>


ng-if更接近ng-switch,因为它们实际上都是从DOM中分离并重新附加html片段。 ng-show只是切换片段的可见性而不将其从DOM中删除。