Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

如何根据条件决定是否添加指令 #76

Open
deepthan opened this issue Jul 27, 2020 · 0 comments
Open

如何根据条件决定是否添加指令 #76

deepthan opened this issue Jul 27, 2020 · 0 comments

Comments

@deepthan
Copy link
Owner

如何根据条件决定是否添加指令

ref

有个指令,我想它有时候被添加上,有时候去除,可以传进去一个布尔值来实现吗?

不可以。没办法直接使用条件判断是否添加指令,不过可以分成两部分,一个添加了指令的区域,一个未添加的,再通过判断显示哪个。

未避免代码冗余将公用的部分抽取出来:

<ng-template #shared>
  <div>这是一个共享的内容</div>
</ng-template>

需要添加指令

<div *ngIf="show" yourDirective>
    <ng-container *ngTemplateOutlet="shared"></ng-container>
</div>

不需要添加指令

<div *ngIf="!show">
    <ng-container *ngTemplateOutlet="shared"></ng-container>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant