Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 7: Can't bind to 'directive' since it isn't a known property of 'element'

I'm trying to create a custom directive and I get this error:

enter image description here

The directive is included in declarations inside @NgModule. Nonetheless is not working. If you need more information on the error just ask. I don't know what's going on.

app.component.html

<input class="text" [appInputFormat]>

input-format.directive.ts

[![import { Directive, HostListener } from '@angular/core';

@Directive({
  selector: '[appInputFormat]'
})
export class InputFormatDirective {

    constructor(){};

  @HostListener('focus')  onFocus(){
    console.log('on Focus');
  }
  @HostListener('blur')  onBlur(){
    console.log('on Blur');
  }
}

app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule} from '@angular/forms';



import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { CourseComponent } from './course/course.component';
import { FavoriteComponent } from './favorite/favorite.component';
import { PanelComponent } from './panel/panel.component';
import { LikeButtonComponent } from './like-button/like-button.component';
import { LikeCountComponent } from './like-count/like-count.component';
import { DirectivesComponent } from './directives/directives.component';
import { HiddenComponent } from './hidden/hidden.component';
import { SwitchcaseComponent } from './switchcase/switchcase.component';
import { ForComponent } from './for/for.component';
import { TrackbyComponent } from './trackby/trackby.component';
import { IfComponent } from './if/if.component';
import { StyleComponent } from './style/style.component';
import { TransopComponent } from './transop/transop.component';
import { InputFormatDirective } from './input-format.directive';

@NgModule({
  declarations: [
    AppComponent,
    CourseComponent,
    FavoriteComponent,
    PanelComponent,
    LikeButtonComponent,
    LikeCountComponent,
    DirectivesComponent,
    HiddenComponent,
    SwitchcaseComponent,
    ForComponent,
    TrackbyComponent,
    IfComponent,
    StyleComponent,
    TransopComponent, 
    InputFormatDirective
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    FormsModule,
  ],
  providers: [
  ],
  bootstrap: [AppComponent]
})
export class AppModule { }
like image 364
Ivan Derlich Avatar asked Jan 22 '19 23:01

Ivan Derlich


People also ask

Can bind to ‘ngforof’ in angular?

Can’t bind to ‘ngForOf’ since it isn’t a known property of ‘div’. In Angular views for displaying object values in a list, we use *ngFor directive to iterate values. This directive takes out each row in the object and creates repetitive blocks of an element which is having NgFor.

How to fix can't bind form to formgroup in angular?

Steps to fix Can't bind to formGroup since it isn't a known property of form error in Angular,1. Import FormsModule, ReactiveFormsModule from @angular/forms in each and every module that uses FormGroup. 2. Add FormsModule and ReactiveFormsModule into imports array of your module.3. In component html file use formGroup selector to bind the form data

Why can't I bind to ngif?

Can't bind to 'ngIf' since it isn't a known property of 'div'. Warning #35774 Sign up for free to subscribe to this conversation on GitHub .

What is the use of ngfor directive in angular?

This directive takes out each row in the object and creates repetitive blocks of an element which is having NgFor. This Angular post is compatible with Angular 4 upto latest versions, Angular 7, Angular 8, Angular 9, Angular 10, Angular 11 & Angular 12


1 Answers

Looking at the documentation here: https://angular.io/guide/attribute-directives

This type of selector:

@Directive({
  selector: '[appInputFormat]'
})

Builds an attribute directive.

It's the brackets ([]) that make it an attribute selector.

So it has to be used as an attribute like this:

<input class="text" appInputFormat>

Brackets when you define the selector, but no brackets in the Html when you use the directive.

like image 92
DeborahK Avatar answered Sep 20 '22 13:09

DeborahK