我可以将条件设​​置为组件选择器Angular 2/4

时间:2017-11-22 13:49:11

标签: angular

是否可以为compnente选择器添加条件?

例如,如果我有两个简单的compnenets:

首先

@Component({
             selector:'app-first'
             templateHtml: 'first.html;
            })
export class FirstComponent {
   check=false;
}

第二

 @Component({
              selector:'app-second'
              templateHtml: 'second.html;
           })

然后,在我的first.html文件中,我设置了这个条件:

<div *ngIf="check">
 <app-second></app-second>
</div>

它总是以任何方式加载我的第二个组件,我可以避免这种情况吗?和 为什么会这样?我没有发现任何关于这个问题的事情。

谢谢。

2 个答案:

答案 0 :(得分:1)

问题不在于您的模板本身。问题在于您的 @Component 装饰器配置。 用 templateUrl 替换 templateHtml。 你可以看到the documentation

答案 1 :(得分:0)

是的有效,你甚至可以做到

<app-second *ngIf="check"></app-second>

您的控制台中是否有任何错误?因为这应该完美。