在我的组件中,我希望根据当前路由参数显示文本,在模板中,我执行了以下操作:
<h1 *ngIf="mode === 'edit'">Modifier</h1> <h1 *ngIf="mode === 'add'">Ajouter</h1>
在Ngoninit函数中,我有:
this.route.params.subscribe(params => { this.id = +params['id']; this.currentMode = params['mode']; })
但是当我访问组件时,元素总是隐藏的,尽管 currentMode 被设定。
currentMode
我怎么解决这个问题?
你不能用 param 在模板中,使用变量名,即 this.currentMode .
param
this.currentMode
<h1 *ngIf="currentMode === 'edit'">Modifier</h1> <h1 *ngIf="currentMode === 'add'">Ajouter</h1>
假设您正在从变量中的参数获取值 this.currentmode当前模式 我会在标签旁边打印出来测试一下,比如:
this.currentmode当前模式
{{currentMode}}