Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular2 Can't bind to DIRECTIVE since it isn't a known property of element

I generated new @Directive by Angular CLI, it was imported it to my app.module.ts

import { ContenteditableModelDirective } from './directives/contenteditable-model.directive';  import { ChatWindowComponent } from './chat-window/chat-window.component';  @NgModule({   declarations: [     AppComponent,     ContenteditableModelDirective,     ChatWindowComponent,     ...   ],   imports: [     ...   ],   ... }) 

and I try to use in my component (ChatWindowComponent)

<p [appContenteditableModel] >     Write message </p> 

even if within directive is only Angular CLI generated code:

 import { Directive } from '@angular/core';   @Directive({    selector: '[appContenteditableModel]'  })  export class ContenteditableModelDirective {   constructor() { }   } 

I got the error:

zone.js:388 Unhandled Promise rejection: Template parse errors: Can't bind to 'appContenteditableModel' since it isn't a known property of 'p'.

I tried almost every possible changes, following this angular docs everything should work but it does not.

Any help?

like image 431
Tomas Javurek Avatar asked Nov 20 '16 15:11

Tomas Javurek


People also ask

Can't bind since it isn't a known property?

What does "Can't bind to 'x' since it isn't a known property of 'y'" mean? link. This error often means that you haven't declared the directive "x" or haven't imported the NgModule to which "x" belongs. Perhaps you declared "x" in an application submodule but forgot to export it.

Can't bind to since it isn'ta known property of DIV?

The solution is actually pretty simple. You have to register CommonModule in the module that you are using. And that's it! The error should disappear.

What are the directives in angular?

Directives are classes that add additional behavior to elements in your Angular applications. Use Angular's built-in directives to manage forms, lists, styles, and what users see.


2 Answers

When wrapping a property in brackets [] you're trying to bind to it. So you have to declare it as an @Input.

import { Directive, Input } from '@angular/core';  @Directive({  selector: '[appContenteditableModel]' }) export class ContenteditableModelDirective {    @Input()   appContenteditableModel: string;    constructor() { }  } 

The important part is, that the member (appContenteditableModel) needs to be named as the property on the DOM node (and, in this case, the directive selector).

like image 78
naeramarth7 Avatar answered Sep 24 '22 13:09

naeramarth7


If you're using a shared module to define the directive make sure it is both declared and exported by the module it's defined in.

// this is the SHARED module, where you're defining directives to use elsewhere @NgModule({   imports: [     CommonModule   ],   declarations: [NgIfEmptyDirective, SmartImageDirective],   exports: [NgIfEmptyDirective, SmartImageDirective] }) 
like image 30
Simon_Weaver Avatar answered Sep 24 '22 13:09

Simon_Weaver