简体   繁体   English

Angular 6指令错误-无法绑定到“ appClickEvent”,因为它不是“ button”的已知属性

[英]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”)

Working Example 工作实例

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.

相关问题 离子错误:无法将 Angular 结构指令绑定到,因为它不是的已知属性 - Ionic Error: Can't bind Angular structural directive to since it isn't a known property of 角度-无法绑定到“属性”,因为它不是“ a”的已知属性 - Angular - Can't bind to 'property' since it isn't a known property of 'a' Angular 7:无法绑定到&#39;directive&#39;,因为它不是&#39;element&#39;的已知属性 - Angular 7: Can't bind to 'directive' since it isn't a known property of 'element' 无法绑定到DIRECTIVE,因为它不是元素Angular AOT的已知属性 - Can't bind to DIRECTIVE since it isn't a known property of element Angular AOT Angular2 无法绑定到 DIRECTIVE,因为它不是元素的已知属性 - Angular2 Can't bind to DIRECTIVE since it isn't a known property of element Angular 11.0.3 不能绑定到 DIRECTIVE,因为它不是元素的已知属性 - Angular 11.0.3 Can't bind to DIRECTIVE since it isn't a known property of element 无法绑定,因为它不是角度分量的已知属性 - Can't bind since it isn't a known property of angular component 无法绑定到“”,因为它不是“ angular 2”的已知属性 - can't bind to '' since it isn't a known property of '' angular 2 无法绑定到“matMenuTriggerFor”,因为它不是“按钮”的已知属性 - Can't bind to 'matMenuTriggerFor' since it isn't a known property of 'button' Angular 6 无法绑定到“*ngIf”,因为它不是已知属性 - Angular 6 Can't bind to '*ngIf' since it isn't a known property
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM