(角2 RC4)角度2の@Inputプロパティで@HostBindingを使用するには?
@HostBindingでは、ホストのプロパティを変更できるはずです。私の質問は、これは@Input()プロパティにも当てはまりますか?もしそうなら、正しい使い方は何ですか?そうでない場合は、これを達成する別の方法がありますか?
私は私の問題を示すために、ここではPlunkerを作っ:
@Component({
selector: 'custom-img',
template: `
<img src="{{src}}">
`
})
export class CustomImgComponent {
@Input() src: string;
}
そして、私は属性ディレクティブでsrcプロパティを養うたい:https://embed.plnkr.co/kQEKbT/
は、私はカスタムコンポーネントがあるとし
@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