我想使用ngFor和ngIf,模板加载动态。
我使用angular.io的这个tutorel来加载我的动态模板: here
我有app.module:
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppRoutingModule } from './app-routing.module';
@NgModule({
declarations: [
AppComponent,
],
imports: [
BrowserModule,
AppRoutingModule,
],
providers: [
],
})
export class AppModule { }
我的第二个模块:
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
@NgModule({
imports: [
CommonModule,
],
providers: [
],
entryComponents: [
Template1Component,
Template2Component,
Template3Component,
],
declarations: [
Template1Component,
Template2Component,
Template3Component,
]
})
export class MyModule { }
通过appRoutingModule在appModule和我的模块之间建立链接:
const appRoutes: Routes = [
{ path: 'site', loadChildren: '../myModule.module#MyModule' },
{ path: '**', component: PageNotFoundComponent },
];
export const AppRoutingModule = RouterModule.forRoot(appRoutes);
我的模板1有:
<ul>
<li *ngfor="let specialite of specialites">
{{specialite.nam}}
</li>
</ul>
一切正常但是当我使用ng时我有这个错误:
ERROR Error: Uncaught (in promise): Error: Template parse errors:
Can't bind to 'ngforOf' since it isn't a known property of 'li'. ("
<ul>
<li [ERROR ->]*ngfor="let specialite of specialites">
{{specialite.nam}}
</li>
"): ng:// ........
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".