当ngIf状态发生变化时,模板元素参考不会更新

时间:2016-08-25 09:56:27

标签: angular

以下是我所说的示例:http://plnkr.co/edit/VxRH7W?p=preview

基本上,我们有一个模板元素引用#theContainer,它在另一个元素(h1)中使用。

容器要呈现与否的条件(*ngIf="theCondition")(我不想使用hidden)。

当条件发生变化时,模板元素引用不会更新,也不会调用ngOnChanges

看起来Angular中的一个错误,我是对的吗?

*您可以在文件src/app.ts

中找到更多信息

1 个答案:

答案 0 :(得分:1)

根据此https://angular.io/docs/ts/latest/guide/template-syntax.html#!#star-template DOM在展开* ngIf

之后将如下所示
<div>
  <h2>Here is it: {{myContainer || 'not assigned yet'}}</h2>

  <button type="button" (click)="theCondition = !theCondition">Switch to {{!theCondition}}</button>

  <template [ngIf]="theCondition">
    <div #myContainer *ngIf="theCondition">
      <p>I see the container's content!</p>

      <p>You should see the container reference in the header.</p>

      <p>Look at src/app.ts</p>
    </div>
  </template>
</div>

现在我们遇到了问题,因为

  

我们可以在同一元素,兄弟元素或任何子元素上引用模板引用变量。

您可以在https://angular.io/docs/ts/latest/guide/template-syntax.html#!#ref-vars

找到它

如果我错了,请纠正我。仍然是Angular的老板。

感谢GünterZöchbauer了解这一点。

相关问题