Can't bind to 'ngForOf' since it isn't a known property of 'tr' (final release)

后端 未结 22 1836
谎友^
谎友^ 2020-11-27 14:37

I\'m using Angular2 Final release (2.1.0). When I want to display a list of companies, I got this error.

in file.component.ts :

public          


        
22条回答
  •  有刺的猬
    2020-11-27 14:55

    I was getting the same error, You can fix through one of this method:

    1. If you don't have any nested module

      a. Import the CommonModule in your App module

      b. Import your Component where you are adding the *ngFor in the App Module, define in declarations

    // file App.modules.ts
    @NgModule({
      declarations: [
        LoginComponent // declarations of your component
      ],
      imports: [
        BrowserModule
        DemoMaterialModule,
        FormsModule,
        HttpClientModule,
        ReactiveFormsModule,
        AppRoutingModule,
        BrowserAnimationsModule,
        ServiceWorkerModule.register('ngsw-worker.js', { enabled: environment.production })
      ],
      providers: [
        ApiService, 
        CookieService, 
        {
          provide: HTTP_INTERCEPTORS,
          useClass: ApiInterceptor,
          multi: true
        }
      ],
      bootstrap: [AppComponent]
    })
    
    

    c. If you are using the separate module file for routing then Import the CommonModule in your Routing module else Import the CommonModule in your App module

    // file app.routing.modules.ts
    import { LoginComponent } from './login/login.component';
    import { CommonModule } from "@angular/common";
    
    const routes: Routes = [
      { path: '', component: LoginComponent },
      { path: 'login', component: LoginComponent }
    ];
    
    @NgModule({
      imports: [RouterModule,RouterModule.forRoot(routes), CommonModule],
      exports: [RouterModule]
    })
    
    1. If you have nested module then perform the 1st step in that particular module

    In my case, the 2nd method solved my issue.
    Hope this will help you

提交回复
热议问题