我通过 Angular CLI 生成了新的 @Directive,它被导入到我的 app.module.ts
import { ContenteditableModelDirective } from './directives/contenteditable-model.directive';
import { ChatWindowComponent } from './chat-window/chat-window.component';
@NgModule({
declarations: [
AppComponent,
ContenteditableModelDirective,
ChatWindowComponent,
...
],
imports: [
...
],
...
})
我尝试在我的组件中使用(ChatWindowComponent)
<p [appContenteditableModel] >
Write message
</p>
即使 inside 指令只是 Angular CLI 生成的代码:
import { Directive } from '@angular/core';
@Directive({
selector: '[appContenteditableModel]'
})
export class ContenteditableModelDirective {
constructor() { }
}
我得到了错误:
zone.js:388 未处理的承诺拒绝:模板解析错误:无法绑定到“appContenteditableModel”,因为它不是“p”的已知属性。
我尝试了几乎所有可能的更改,按照这个 角度文档,一切都应该工作,但它没有。
有什么帮助吗?
原文由 Tomas Javurek 发布,翻译遵循 CC BY-SA 4.0 许可协议
将属性包装在括号中时
[]
您正在尝试绑定到它。因此,您必须将其声明为@Input
。重要的部分是,成员(
appContenteditableModel
)需要被命名为 DOM 节点上的属性(在本例中为指令选择器)。