简体   繁体   English

为什么“ [hidden]”有效,但是“ * ngIf”不适用于Angular2中的ngOnInit()?

[英]Why “[hidden]” works, but “*ngIf” does not work for ngOnInit() in Angular2?

For the following code, I used hidden, it called ngOnInit() when I routed between this two route. 对于以下代码,我使用了隐藏的,当我在这两条路由之间路由时,它称为ngOnInit()。 However, when I used "*ngif", ngOnInit() was never called. 但是,当我使用“ * ngif”时,从未调用ngOnInit()。 I wondering why it happened. 我想知道为什么会这样。 Also, I want to "*ngif" if it is possible since "[hidden]" can conflict with CSS. 另外,如果“ [隐藏]”可能与CSS冲突,我希望尽可能使用“ * ngif”。

<div class="container">
                <div [hidden]="!(router.url == '/info/userinfo')">
                  <div class="catalog" >
                    <button [routerLink]="['/info/userinfo']"><h2>userinfo</h2></button>
                </div>
                <div [hidden]="!(router.url == '/info/question')">
                  <div class="catalog">
                    <button [routerLink]="['/info/question']"><h2>question</h2></button>
                  </div>
                </div>
     </div>

userinfo and question are two components and children of info. userinfo和问题是info的两个组成部分和子元素。 I guess it has something relate to route, since if I go from '/home' to '/info/question', ngOnInit() will be called. 我想它与路由有关,因为如果我从“ / home”转到“ / info / question”,则会调用ngOnInit()。 However, if I go from '/info/userinfo' to '/info/question', both ngOnInit() will not be called. 但是,如果我从“ / info / userinfo”转到“ / info / question”,则不会同时调用ngOnInit()。

因为* ngIf when的值为false不会创建您的组件,而是将其隐藏仅隐藏该组件,但仍会创建它。

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

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