英文:
Switch/Case to change Title not working in Angular
问题
appConfig.json
"env": "stage",
app.component.ts
env: string;
constructor(
private configService: ConfigService
) {}
ngOnInit() {
this.env = this.configService.config.env;
};
app.component.html
<ng-template [ngSwitch]="env">
<span *ngSwitchCase="'production'">标题 生产环境</span>
<span *ngSwitchCase="'stage'">标题 阶段环境</span>
<span *ngSwitchDefault>默认标题</span>
</ng-template>
英文:
I need to change the title depending on the environment value. But now no titles are displayed.
appConfig.json
"env": "stage",
app.component.ts
env: string;
constructor(
private configService: ConfigService
) {}
ngOnInit() {
this.environment = this.configService.config.env;
};
app.component.html
<ng-template [ngSwitch]="env">
<span *ngSwitchCase="'production'">Title Production</span>
<span *ngSwitchCase="'stage'">Title Stage</span>
<span *ngSwitchDefault>Title Default</span>
</ng-template>
答案1
得分: 0
尝试使用ng-container(或div)代替ng-template作为ngSwitch指令的宿主元素。
<ng-container [ngSwitch]="env">
<span *ngSwitchCase="'production'">标题 生产环境</span>
<span *ngSwitchCase="'stage'">标题 阶段</span>
<span *ngSwitchDefault>标题 默认</span>
</ng-container>
英文:
Try using ng-container(or div) instead of ng-template as a host element for ngSwitch directive.
<ng-container [ngSwitch]="env">
<span *ngSwitchCase="'production'">Title Production</span>
<span *ngSwitchCase="'stage'">Title Stage</span>
<span *ngSwitchDefault>Title Default</span>
</ng-container>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论