简体   繁体   English

带有递归指令的ng-if无法正常工作

[英]ng-if with recursive directives does not work as expected

I have two recursive directives inside of ui-bootstrap tabs. 我在ui-bootstrap选项卡中有两个递归指令。 For performance reasons I only want to actually load the directive when it's respective tab is active. 出于性能原因,我只想在相应选项卡处于活动状态时实际加载该指令。 So I use ng-if to on the directive like so: 所以我在指令上使用ng-if像这样:

<dave ng-if="activeTab === 0"></dave>
...
<bob ng-if="activeTab= === 1"></bob>

Now if you go to Tab 0 you see "dave". 现在,如果转到选项卡0,则会看到“ dave”。 Then go to Tab 1 you see "bob". 然后转到选项卡1,您会看到“ bob”。 Go back to Tab 0 you still see "bob". 返回标签0,您仍然会看到“ bob”。 If I remove the ng-if all works as expected: dave, bob, dave. 如果我删除了ng-如果所有工作都按预期进行:dave,bob和dave。

I need to be able to only render the directives when the tab is active. 我需要仅在选项卡处于活动状态时才能呈现指令。 I have tried putting the directives inside a div and using ng-if on that like so: 我试过将指令放入div并在其上使用ng-if,如下所示:

<div ng-if="activeTab === 0"><dave></dave></div>

But still have the same problem. 但是仍然有同样的问题。

Codepen of problem 问题的笔

I was able to get the tabs to properly go back and forth by doing the following: 通过执行以下操作,我可以使标签正确地来回移动:

<div class="container" ng-app="app">
  <uib-tabset active=activeTab>
    <uib-tab index=0>
      <uib-tab-heading>Tab One</uib-tab-heading>
      <dave ng-if="active === index" recurse="true"></dave>
    </uib-tab>
    <uib-tab index=1>
      <uib-tab-heading>Tab Two</uib-tab-heading>
      <bob ng-if="active === index" recurse="true"></bob>
    </uib-tab>
  </uib-tabset>
</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM