(Angular 2 RC4)如何使用@HostBinding和Angular 2中的@Input屬性?
隨着@HostBinding我們應該能夠修改主機的屬性,對吧?我的問題是,這是否也適用於@Input()屬性,如果是這樣,什麼是正確的用法?如果沒有,是否有另一種方法來實現這一目標?
我做了一個Plunker這裏來說明我的問題:https://embed.plnkr.co/kQEKbT/
假設我有一個自定義組件:
@Component({
selector: 'custom-img',
template: `
<img src="{{src}}">
`
})
export class CustomImgComponent {
@Input() src: string;
}
我想src屬性與屬性指令飼料:
@Directive({
selector: '[srcKey]'
})
export class SrcKeyDirective implements OnChanges {
@Input() srcKey: string;
@HostBinding() src;
ngOnChanges() {
this.src = `https://www.google.com.mt/images/branding/googlelogo/2x/${this.srcKey}_color_272x92dp.png`;
}
}
爲什麼不能更改自定義組件的[src]輸入屬性?
@Component({
selector: 'my-app',
directives: [CustomImgComponent, SrcKeyDirective],
template: `<custom-img [srcKey]="imageKey"></custom-img>`
})
export class AppComponent {
imageKey = "googlelogo";
}
謝謝!
謝謝你的快速反應。我在這裏發佈了一個錯誤報告:https://github.com/angular/angular/issues/10499 – Laurens