So I'm obviously new to ng2, as are a lot of people currently. With the release of the first version I have been learning a little. I am starting to get some of the more "ng2" ways of thinking down.
However, something as simple as ngIf
I can't get to work.
This is my view:
<div *ngIf="testVariable" class="checkbox">
<label>
<input type="checkbox" formControlName="rememberMe" value="remember-me"> Remember me
</label>
</div>
In my component.ts:
import { Component, OnInit } from '@angular/core';
import { FormGroup, FormControl, FormBuilder, Validators } from '@angular/forms';
...
ngOnInit() {
this.loginForm = this._fb.group({
username: ['', [<any>Validators.required]],
password: ['', [<any>Validators.required]],
rememberMe: []
});
this.testVariable = false;
}
Do I have to import something extra? I have seen pre-release ng2 examples importing CORE_COMPONENTS
but I can't find a recent example with that in.
My module declaration:
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { LoginComponent } from './login.component';
@NgModule({
imports: [ ReactiveFormsModule, CommonModule ],
exports: [ LoginComponent ],
declarations: [ LoginComponent ],
providers: [ ],
})
export class LoginModule { }
Solution for Angular Can't bind to 'ngIf' since it isn't a known property of 'div' There are multiple ways to fix this. Incorrect ngIf syntax One way, This is due to an error caused due to misspelling capital 'I' for div. To solve this, Import CommonModule or BroswerModule or both into app.
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.
There is no need for any import for the NgIf to be used. In app. component. ts define the variable for which condition is to be checked with ngIf directive.
It's possible that the issue is caused by incorrect syntax or the absence of a dependent module. This is also caused by missing modules in parent and child modules that are lazy-loaded. Import BrowserModule in the parent module and CommonModule in the child module to fix the problem.
You have to add CommonModule
in your application's @NgModule
's imports
declaration as below :
@NgModule({
imports : [
CommonModule
]
})
export default class AppModule {}
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