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

后端 未结 22 2022
谎友^
谎友^ 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 15:05

    A lot of answers seem to converge by importing CommonModule in other(new/custom) modules.
    This step only isn't enough in all situations.

    The full solution consist in two steps:

    1. Make directives NgIf, NgFor etc visible to your project.
    2. Reassemble everything in a correct way in the main component (app.module.ts)

    Point 1
    BrowserModule in main module seems to be enough for having access to NgFor. Angular Documentation stands it here: .

    CommonModule Exports all the basic Angular directives and pipes, such as NgIf, NgForOf, DecimalPipe, and so on. Re-exported by BrowserModule,

    See also accepted answer here: CommonModule vs BrowserModule in angular

    Point 2
    The only changes needed (in my case) are the followings:

    1. import Module OtherModule
    2. import Component OtherComponent
    3. ng build (important!)
    4. ng serve

    app.module.ts

    @NgModule({
        imports: [
            BrowserModule,
            OtherModule
        ],
        declarations: [OtherComponent, AppComponent],
        bootstrap: [AppComponent]
    })
    export class AppModule {
    }
    

    other.html

    other.component.ts

    @Component({
        selector: 'other-component',
        templateUrl: './other.html'
    })
    export class OtherComponent {
    }
    

    app.module.ts

    @NgModule({
        imports: [],
        providers: []
    })
    export class OtherModule{
    }
    

提交回复
热议问题