这个错误是因为*ngIf条件语句的约定绑定了一个模板变量,但它是指的一个HTML元素。为解决这个问题,需要将这个变量绑定到一个模板引用变量上,而不是一个DOM元素上。
以下是一种解决方法,假设我们的问题是在组件中一段代码片段中。
在HTML模板中:
This template will be displayed if the condition is true
This template will be displayed if the condition is false
在这里,我们将*ngIf绑定到了一个模板引用变量(template1和template2)上,而不是一个HTML元素上。我们已经定义了这些变量,然后将它们添加到< ng-container > 标签中。如果条件满足,将会展示template1模板的内容,否则将会展示template2的内容。
在组件中:
export class AppComponent {
myInput: string = '';
}
我们定义了一个名为myInput的变量并将其初始值设置为空字符串。现在我们可以运行它,问题就可以解决了。
上一篇:Angular12-'onbeforeunload'事件未触发
下一篇:Angular12-Can'tbindto'ngModel'sinceitisn'taknownpropertyof'input'