[英]Angular 6 Directive Error - Can't bind to 'appClickEvent' since it isn't a known property of 'button'
Created a module with a directive inside that will catch a click event from a button. 创建了一个内部带有指令的模块,该模块将捕获来自按钮的click事件。
click.directive.ts click.directive.ts
import { Directive, ElementRef, HostListener, Input } from '@angular/core';
@Directive({
selector: '[appClickEvent]'
})
export class ClickDirective {
private el:any;
constructor(
private elem: ElementRef,
) {}
@Input() appClickEvent: any;
@HostListener('click', ['$event']) clickEvent(event:any) {
console.log(this.appClickEvent) // {label: 'Foo'}
}
}
Usage in a component 组件中的用法
I want to get the data {label: 'Foo'}
and pass it to the @HostListener
in the click.directive.ts
. 我想获取数据{label: 'Foo'}
并将其传递给@HostListener
中的click.directive.ts
。
<button [appClickEvent]="{label: 'Foo'}">Click Foo</button>
my.module.ts my.module.ts
import { CommonModule } from '@angular/common';
import { ClickDirective } from './click.directive';
@NgModule({
imports: [
CommonModule
],
exports: [
ClickDirective
],
declarations: [
ClickDirective
]
})
@NgModule()
export class McAnalyticsModule {}
app.module.ts app.module.ts
// ... other modules
import { MyModule } from '@app/my/my.module';
@NgModule({
imports: [
// .. .other modules
MyModule,
],
declarations: [AppComponent],
bootstrap: [AppComponent]
})
export class AppModule { }
Error 错误
Can't bind to 'appClickEvent' since it isn't a known property of 'button'. 无法绑定到“ appClickEvent”,因为它不是“ button”的已知属性。 ("][appClickEvent]="{label: 'Foo'}">Foo") (“] [appClickEvent] =” {label:'Foo'}“> Foo”)
click.directive.ts: click.directive.ts:
import { Directive, ElementRef, HostListener, Input } from '@angular/core';
@Directive({
selector: '[appClickEvent]'
})
export class ClickDirective {
private el:any;
constructor(
private elem: ElementRef,
) {}
@Input() appClickEvent: any;
@HostListener('click', ['$event']) clickEvent(event:any) {
console.log("your output");
console.log(this.appClickEvent)
}
}
app.module.ts app.module.ts
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { AppComponent } from './app.component';
import { HelloComponent } from './hello.component';
import { ClickDirective } from './click.directive';
@NgModule({
imports: [ BrowserModule, FormsModule ],
declarations: [ AppComponent, HelloComponent,ClickDirective ], exports: [ClickDirective],
bootstrap: [ AppComponent ]
})
export class AppModule { }
html: 的HTML:
<button appClickEvent="{label: 'Foo'}">Click Foo</button>
You should inject McAnalyticsModule
in app.module.ts
Not MyModule
. 您应该在app.module.ts
不是MyModule
注入McAnalyticsModule
。 So angular didn't find the McAnalyticsModule
. 因此, McAnalyticsModule
找不到McAnalyticsModule
。
Include McAnalyticsModule
in app.module.ts
like this: 像这样在app.module.ts
包含McAnalyticsModule
:
import { McAnalyticsModule } from '@app/my/my.module'; //<== Remove MyModule class
@NgModule({
imports: [
// .. .other modules
McAnalyticsModule, //<== Remove MyModule class
],
declarations: [AppComponent],
bootstrap: [AppComponent]
})
export class AppModule { }
Here is working example: Angular 6 Directive 这是工作示例: Angular 6指令
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.