【发布时间】:2019-01-03 21:32:45
【问题描述】:
我正在尝试创建一个带有取消和发送按钮的组件,以避免在每个表单上出现 c&p,但我找不到将函数作为参数传递给组件选择器的方法
HTML:
<btn-submit [cancelFunction]='test' [acceptFunction]='test'></btn-submit>
TS 家长:
test = () => console.log("this is a test");
TS 孩子:
import { Component, Input } from '@angular/core';
@Component({
selector: 'btn-submit',
styleUrls: ['./btn.component.scss'],
template: `
<button class="btn" click="cancelFunction()">
<fa name="times"></fa>
</button>
<button class="btn" click="acceptFunction()">
<fa name="check"></fa>
</button>
`
})
export class BtnSubmitComponent {
@Input() cancelFunction: any;
@Input() acceptFunction: any;
}
【问题讨论】:
-
你应该改用
Output -
当我认为您需要使用属性绑定时,您正在使用事件绑定。如果要将函数 in 传递给嵌套的子组件,则需要
Input装饰器,但需要使用属性绑定对其进行绑定。另外,我认为您想传入函数,而不是执行函数,因此您可能需要关闭()。
标签: angular input components selector