415

Can't bind to 'ngIf' since it isn't a known property of 'div'.

The element is <div [ngIf]="isAuth" id="sidebar">

And the component is:

import SessionService from '../session/session.service';
import { Component } from '@angular/core';

@Component({
  providers: [],
  selector: 'navbar-left',
  styles: [require('./navbar-left.scss')],
  template: require('./navbar-left.html'),
})
export default class NavbarLeftComponent {
  public isAuth: boolean = false;

  constructor(private sessionService: SessionService) {
    this.isAuth = sessionService.sessionIsAuth();
  }
}

Not sure what exactly I'm doing wrong? This is a child component. In the parent component aka App component the ngIf works fine. Angular RC5

isherwood
  • 58,414
  • 16
  • 114
  • 157
Ivaylo Ivanov
  • 4,333
  • 2
  • 12
  • 16

3 Answers3

570

Just for anyone who still has an issue, I also had an issue where I typed ngif rather than ngIf (notice the capital 'I').

dudewad
  • 13,215
  • 6
  • 37
  • 46
  • 64
    `nfIf` in my case, sometimes... :facepalm: – Brad Adams Jun 27 '17 at 23:59
  • It can also happen if you write: *ngIf="{{condition}}". You must not write the {{ }}. *ngIf="condition" is working. – Nis Jul 18 '19 at 15:29
  • 1
    yes 80% people were using *ngif ---> ngIf – pankaj Jan 20 '21 at 09:00
  • ngIf was a bad idea for naming, it should have been ngif , but strict nomenclatures standards forced the naming to be ngIf and ngFor instead of ngfor and ngif – Juan Vilar Feb 04 '21 at 12:09
  • The reason being is that this single comment has now 400 upvotes, so there have been many confused people who have lost at least 5 minutes trying to figure out what was wrong – Juan Vilar Feb 04 '21 at 12:09
  • 3
    I don't think "blaming camel case" is really accurate. 400 upvotes on a 5 year old post isn't that severe. I think it just indicates that people are accidentally typing typos. When I realized this it was not because I didn't know how to use ngIf, it was because of a typo. Same for most people here. – dudewad Feb 10 '21 at 21:50
  • I was almost frustrated by trying all the ways like [BrowserModule], [CommonModule] etc., and finally the capital case "I" saved my day. – Siva Makani Feb 23 '21 at 11:03
  • ngIF in my case :D – orchidrudra Sep 22 '21 at 18:16
  • NgIf in my case :/ – Mill Oct 11 '21 at 12:56
  • I was looking at my code an it said *ngIf* but an inner component had a *ngif*, – Mauricio Gracia Gutierrez Feb 12 '22 at 15:08
  • Its incredible how many people have found this comment useful, its by far my single most upvoted response. Glad it helped @fruitloaf – dudewad Feb 17 '22 at 06:03
  • In my case was because I didnt add my Component into the module in declarations – Hussein Hn Oct 27 '22 at 08:58
569

If you are using RC5 then import this:

import { CommonModule } from '@angular/common';  
import { BrowserModule } from '@angular/platform-browser';

and be sure to import CommonModule from the module that is providing your component.

 @NgModule({
    imports: [CommonModule],
    declarations: [MyComponent]
  ...
})
class MyComponentModule {}
N8allan
  • 2,138
  • 19
  • 32
null canvas
  • 10,201
  • 2
  • 15
  • 18
59

Instead of [ngIf] you should use *ngIf like this:

<div *ngIf="isAuth" id="sidebar">
Peter Salomonsen
  • 5,525
  • 2
  • 24
  • 38