Angular 2 ngIf用于嵌套组件

时间:2016-11-16 12:53:17

标签: angular angular2-directives

我只是学习angular2并且对嵌套组件的ngIf行为有所了解。我有2个模块:

ProfileModule

import {NgModule} from "@angular/core";
import {ProfileDetailComponent} from "./profile-detail/profile-detail.component";
import {ProfileService} from "./profile.service";
import {SharedModule} from "../shared/shared.module";
import {ProfileCareerListComponent} from "./profile-career-list/profile-    career-list.component";
import {ProfileCareerDetailComponent} from "./profile-career-detail/profile-career-detail.component";

@NgModule({
    imports: [
        SharedModule
    ],
    declarations: [
        ProfileDetailComponent,
        ProfileCareerListComponent,
        ProfileCareerDetailComponent
    ],
    exports:[
        SharedModule
    ],
    providers:[
        ProfileService
    ]
})
export class ProfileModule{}

SharedModule:

import {NgModule} from "@angular/core";
import {CommonModule} from "@angular/common";
import {FormsModule} from "@angular/forms";

@NgModule({
    declarations:[
        DateDiffPipe
    ],
    imports:[
        CommonModule
    ],
    exports:[
        CommonModule,
        FormsModule
    ]
})
export class SharedModule{}

我在ProfileModule中声明了3个组件。

ProfileDetailComponent包含代表ProfileCareerListComponent的代码:

...
<div class="panel-body">
    <sn-career-list></sn-career-list>
</div>
...

ProfileCareerListComponent包含用于表示ProfileCareerDetailComponent的代码:

<div *ngIf="careerList"> /* this ngIf is work */
    <sn-career-detail *ngFor="let item of careerList" [career]="item"></sn-career-detail>
</div>

ProfileCareerDetailComponent包含ngIf指令,但它不起作用:

...
<small *ngIf="career.RetirementDate"> - {{career.RetirementDate | date:'MMMM y'}}</small>
...

当我尝试启动此代码时,我遇到了错误:

Can't bind to 'ngif' since it isn't a known property of 'small'. (" *ngIf="career.RetirementDate"> - {{career.RetirementDate | date:'MMMM y'}}</small>

如何在导出的CommonModule中导入/导出SharedModule,以便在ProfileCareerDetailComponent中使用ngIf?也许我必须在其他模块中导入其他内容?或者这是不可能的?

0 个答案:

没有答案