Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2 - ngIf - Not a known property of div

Tags:

angular

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 { }
like image 223
Aleski Avatar asked Oct 03 '16 07:10

Aleski


People also ask

Can't bind to ngIf since it isn't a known property of Div angular 11?

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.

Can't bind to since it isn't a 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 should I import into ngIf?

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.

Can't bind to ngIf since it isn't a known property of APP child?

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.


1 Answers

You have to add CommonModule in your application's @NgModule's imports declaration as below :

@NgModule({
  imports     : [
    CommonModule
  ]
})
export default class AppModule {}
like image 165
ranakrunal9 Avatar answered Nov 15 '22 06:11

ranakrunal9