Angular 角度模板可以是可变的吗?
我在组件中声明了一个变量,我们称它为“myVar”。根据这个变量,我想显示一个特定的ng模板。以下是一个示例组件模板:Angular 角度模板可以是可变的吗?,angular,typescript,angular6,angular-ng-if,Angular,Typescript,Angular6,Angular Ng If,我在组件中声明了一个变量,我们称它为“myVar”。根据这个变量,我想显示一个特定的ng模板。以下是一个示例组件模板: <div *ngIf="myVar; then myVar; else nothing"></div> <ng-template #foo>My Foo-Content</ng-template> <ng-template #nothing>No Content</ng-template> 我的食物内容
<div *ngIf="myVar; then myVar; else nothing"></div>
<ng-template #foo>My Foo-Content</ng-template>
<ng-template #nothing>No Content</ng-template>
我的食物内容
没有内容
有没有可能做到这一点?我希望“myVar”为null或名称为“foo”,但上面的代码当然不会计算“myVar”
或者有什么办法可以解决这个问题?在我的实际应用程序中,“myVar”有大约10个不同的值,但内容并不足以使其成为自己的组件
我需要为此制作10个不同的*ngIf吗?:(似乎就是你要找的
<div [ngSwitch]="myVar">
<div *ngSwitchCase="'Foo'">
Content
</div>
<div *ngSwitchCase="'Bar'">
Content
</div>
<div *ngSwitchDefault>
Empty or fallback
</div>
</div>
所容纳之物
所容纳之物
空降
NgSwitch?@JBNizet这是一个好主意,它工作起来很有魅力!如果你能给出一个小的答案,我会接受的。我添加了我的示例代码。令我非常尴尬的是,我甚至都不知道NgSwitch。它非常棒,还捕捉到了模板找不到时可能发生的任何异常。非常感谢。