32

I can able to run through locally. Getting error only in production build.

I have used

 import { CommonModule } from '@angular/common';
 imports:      [  CommonModule ]

Full error is shown below.

client:101 Template parse errors:enter code here`Can't bind to 'ngIf' since it isn't a known property of 'div'. 
("move" class="transport-remove">Remove</a></div>
         <div id="carTypeDiv_1" class="veh-inv-out" [ERROR ->]*ngIf="vehicleData.vesselType == 'road'">
            <ul id="carTypeList_1" class="veh-slides">
    "): VehicleDirective@10:52
Property binding ngIf not used by any directive on an embedded template. Make sure that the property name is spelled correctly and all directives are listed in the "@NgModule.declarations". ("t:" (click)="removeField($event)" title="Remove" class="transport-remove">Remove</a></div>
         [ERROR ->]<div id="carTypeDiv_1" class="veh-inv-out" *ngIf="vehicleData.vesselType == 'road'">
            <ul "): VehicleDirective@10:9
Can't bind to 'ngForOf' since it isn't a known property of 'option'. ("l)]="vehicleData.makeSelect" (change)="appendModel($event.target.value)">
                  <option [ERROR ->]*ngFor="let make of vehicle.makes">{{make}}</option>
               </select>
            </div>
"): VehicleDirective@28:26
Property binding ngForOf not used by any directive on an embedded template. Make sure that the property name is spelled correctly and all directives are listed in the "@NgModule.declarations". ("[(ngModel)]="vehicleData.makeSelect" (change)="appendModel($event.target.value)">
                  [ERROR ->]<option *ngFor="let make of vehicle.makes">{{make}}</option>
               </select>
            </d"): VehicleDirective@28:18
Can't bind to 'ngForOf' since it isn't a known property of 'option'. ("t" id="modelSelect" [(ngModel)]="vehicleData.modelSelect" class="prefixbox">
               <option [ERROR ->]*ngFor="let model of vehicle.models">{{model}}</option>
            </select></div>
            <br c"): VehicleDirective@36:23
Property binding ngForOf not used by any directive on an embedded template. Make sure that the property name is spelled correctly and all directives are listed in the "@NgModule.declarations". ("delSelect" id="modelSelect" [(ngModel)]="vehicleData.modelSelect" class="prefixbox">
               [ERROR ->]<option *ngFor="let model of vehicle.models">{{model}}</option>
            </select></div>
         "): VehicleDirective@36:15
Can't bind to 'ngForOf' since it isn't a known property of 'option'. ("refixbox">
                        <option value="">SELECT</option>
                        <option [ERROR ->]*ngFor="let year of vehicle.years">{{year}}</option>
                     </select>
                 "): VehicleDirective@47:32
Property binding ngForOf not used by any directive on an embedded template. Make sure that the property name is spelled correctly and all directives are listed in the "@NgModule.declarations". ("class="prefixbox">
                        <option value="">SELECT</option>
                        [ERROR ->]<option *ngFor="let year of vehicle.years">{{year}}</option>
                     </select>
         "): VehicleDirective@47:24
Can't bind to 'ngForOf' since it isn't a known property of 'option'. ("ateSelect" [(ngModel)]="vehicleData.stateSelect" class="prefixbox">
                        <option [ERROR ->]*ngFor="let state of vehicle.regStates">{{state}}</option>
                     </select>
           "): VehicleDirective@55:32
Property binding ngForOf not used by any directive on an embedded template. Make sure that the property name is spelled correctly and all directives are listed in the "@NgModule.declarations". ("" id="stateSelect" [(ngModel)]="vehicleData.stateSelect" class="prefixbox">
                        [ERROR ->]<option *ngFor="let state of vehicle.regStates">{{state}}</option>
                     </select>
   "): VehicleDirective@55:24
Can't bind to 'ngForOf' since it isn't a known property of 'option'. ("lorSelect" [(ngModel)]="vehicleData.colorSelect" class="prefixbox">
                        <option [ERROR ->]*ngFor="let color of vehicle.colors">{{color}}</option>
                     </select>
              "): VehicleDirective@68:32
Property binding ngForOf not used by any directive on an embedded template. Make sure that the property name is spelled correctly and all directives are listed in the "@NgModule.declarations". ("" id="colorSelect" [(ngModel)]="vehicleData.colorSelect" class="prefixbox">
                        [ERROR ->]<option *ngFor="let color of vehicle.colors">{{color}}</option>
                     </select>
      "): VehicleDirective@68:24

What is the reason. I have verified many solution. could not find the soultion. Same code is working fine with local.

raj m
  • 1,863
  • 6
  • 21
  • 37
  • 1
    Add Your HTML template code where you have use ngIf on div element – mayur Feb 06 '17 at 09:17
  • Does this answer your question? [Can't bind to 'ngIf' since it isn't a known property of 'div'](https://stackoverflow.com/questions/39058075/cant-bind-to-ngif-since-it-isnt-a-known-property-of-div) – dota2pro Apr 23 '21 at 08:57

8 Answers8

93

I had exactly the same error, and I had included both CommonModule and BrowserModule and yet I still saw the same error message on my browser.

Finally, I found the cause of my problem was that I forgot to add my component to app.module.ts, in case someone else is dealing with the same behavior.

jmanz
  • 1,031
  • 1
  • 8
  • 6
39

After adding browser module its working fine.

 import { BrowserModule } from '@angular/platform-browser';
 @NgModule({
     imports: [BrowserModule ]
  })
raj m
  • 1,863
  • 6
  • 21
  • 37
  • 14
    It really looks like Angular's error handling could 'know' (or guess) that ngIf is one of it's own directives, and 'know' that it is in BrowserModule. The error message in the browser console could suggest 'Have you imported BrowserModule'? Probably the biggest shortcoming in Angular IMHO is the error messages can be less than helpful when things go wrong. – Wallace Howery Jan 01 '18 at 18:28
  • 1
    Leave it to Angular where you need to close your eyes, touch your nose, and hop on your left foot to make a default feature work properly. – nesterenes Dec 20 '21 at 21:05
35

This error can also occur if your component is NOT included in the declarations section of the module:

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';            // <-- required

import { InventoryRoutingModule } from './inventory-routing.module';
import { InventoryComponent } from './pages/inventory/inventory.component';

@NgModule({
  declarations: [InventoryComponent],                      // <-- In my case, this was missing
  imports: [CommonModule, InventoryRoutingModule]
})
export class InventoryModule {}
psyklopz
  • 2,283
  • 4
  • 24
  • 29
18

Other solution if none of the above works is just to restart your ng serve, that worked for me. Or maybe try this method first before pulling your hairs out.

PS: This is not the OP's issue as he's on production, this is only for development environment.

Shadoweb
  • 5,812
  • 1
  • 42
  • 55
10

You should add CommonModule either in the root component or the related component,

import { CommonModule } from "@angular/common";

Also, add CommonModule to the imports property of @NgModule and your component to the declarations property.

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    CommonModule
  ],
  providers: [],
    bootstrap: [AppComponent]
  })
psyklopz
  • 2,283
  • 4
  • 24
  • 29
Vijayanath Viswanathan
  • 8,027
  • 3
  • 25
  • 43
2

In my case, I already had a component with the same name in app.module.ts, which led to my new component not being parsed correctly and resulting in "can't bind to 'ngIf'", basically same issue as above with missing declaration in app.module.ts

solution:

existing component:

import { DialogComponent } from './common/button-popup/dialog/dialog.component';

I had to add an alias for the new component

import { DialogComponent } from './common/button-popup/dialog/dialog.component';
import { DialogComponent as DialogGenericComponent } from './common/dialog/dialog.component';
2

if your project working fine but underline with the error. update Angular language service extension. to update press Ctrl + Shift + x and search Angular language service then update the extension and reload through the extension

enter image description here

1

If your component is standalone for ex -

@Component({
selector: 'app-delete-alert',
templateUrl: './delete-alert.component.html',
styleUrls: ['./delete-alert.component.scss'],
standalone: true,
})

Then import CommonModule in the same file.

@Component({
selector: 'app-delete-alert',
templateUrl: './delete-alert.component.html',
styleUrls: ['./delete-alert.component.scss'],
standalone: true,
imports:[CommonModule]
})
Mrunal Tupe
  • 140
  • 4
  • 6