I'm trying to create a custom directive and I get this error:
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 { }
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.
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
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 .
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With