2016-10-15 43 views
2

這是怎麼了我的主機,綁定到評爲NG2-RC4的父組件「禁用」按鈕屬性:Angular2決賽:HostBinding不工作了

@Component({ 
    selector: "nav-next", 
    template: ` 
    <div class="nav-next-directive" (click)="onClick($event)"> 
     <button color="primary" class="primary" [attr.disabled]="disabled"> 
      <ion-spinner *ngIf="ngIf === true" icon="lines"></ion-spinner> 
      {{buttonTitle}} 
     </button> 
    </div>` 
}) 

export class NavNextDirective { 

    @HostBinding("attr.disabled"); 
    @Input() isValid: boolean; 

這不工作了,(NGC抱怨)我現在已經在部分上述更改爲得到了一半:

@HostBinding("attr.disabled") isValid: boolean = true; 
@Input() isValid: boolean; 

但是NGC說:

Can't bind to 'isValid' since it isn't a known property of 'nav-next'. 
1. If 'nav-next' is an Angular component and it has 'isValid' input, then verify that it is part of this module. 
2. If 'nav-next' is a Web Component then add "CUSTOM_ELEMENTS_SCHEMA" to the '@NgModule.schemas' of this component to suppress this message. 
(" 
    <ion-list> 
     <nav-next (click)="save()" 
        [ERROR ->][isValid]="goalForm.valid" 
        [serverWaiting]="serverWaiting" 
        button"): [email protected]:18 

現在Hostbinding有什麼想法?

回答

1

該錯誤與主機綁定無關。這是關於NavNextDirective不是已知的組件嘗試使用它

NavNextDirective需要在同一範圍因爲它正在使用的組件。這意味着你可以在同一個模塊

@NgModule({ 
    declarations: [ NavNextDirective, ComponentThatUsesNavNextDirective ] 
}) 
class SomeModule {} 

或者在聲明它,如果NavNextDirective是由不同的模塊來使用,無論是申報和出口指令在自己的模塊或共享的模塊中,並導入模塊成有使用該指令部件模塊

@NgModule({ 
    declarations: [ NavNextDirective ], 
    exports: [ NavNextModuleDirective ] 
}) 
class NavNextModule {} 

// OR 

@NgModule({ 
    declarations: [ NavNextDirective ], 
    exports: [ NavNextModuleDirective ] 
}) 
class SharedModule {} 

然後將它導入

@NgModule({ 
    imports: [ SharedModule ] 
    declarations: [ ComponentThatUsesNavNextDirective ] 
}) 
class ModuleWithComponentThatUsesNavNextDirective {} 

這是一個誤解,我覺得有些人有組件/指令只需要一次導入到應用程序模塊中,其他所有模塊都可以使用它。這是不是這樣

@NgModule({ 
    imports: [ ModuleWithComponentThatUsesNavNextDirective ], 
    declarations: [ NavNextDirective ] 
}) 
class AppModule {} 

這裏,ModuleWithComponentThatUsesNavNextDirective不能看到AppModule宣佈NavNextDirective。它或者需要聲明指令本身,或者指令的模塊。但請注意,一個組件只能由任何模塊聲明一次。因此,如果組件要被許多模塊使用,那麼您應該專門爲該組件創建一個模塊,或創建一個包含一組可重用組件的共享模塊。