我試圖將父組件中的函數綁定到子組件上的屬性。Angular2通過屬性將函數傳遞給指令
這是我
@Component({
selector: 'awesome',
templateUrl: 'awesome.html'
})
export class AwesomeComponent {
@Input() callback: Function;
ngOnInit() {
this.callback();//Error, this.callback is not a function, but contains a string value on the fuction call
}
}
這是如何我使用它
<awesome callback="nameOfFuncFromAnotherComponent"></awesome>
,但它似乎沒有工作
由於部件'awesome'有一個'callback'輸入屬性聲明的,'回調= 「nameOfFuncFromAnotherComponent」'將字符串'nameOfFuncFromAnotherComponent'綁定到組件的'callback' _property_ (而不是屬性)。這是常量的某種角度捷徑/特殊語法(我不喜歡)。 –
@StevenYates,'callback =「{{nameOfFuncFromAnotherComponent}}」'不起作用,因爲它基本上調用'nameOfFuncFromAnotherComponent.toString()'並將其賦值給'callback'屬性。 '{{}}'綁定總是分配一個字符串結果。 –
感謝您的信息 –