【发布时间】:2016-10-04 18:16:08
【问题描述】:
可以通过@Input 将数据从父级发送到子级,或者使用@Output 从子级调用父级的方法,但我想完全相反,即调用来自父母的孩子的方法。基本上是这样的:
@Component({
selector: 'parent',
directives: [Child],
template: `
<child
[fn]="parentFn"
></child>
`
})
class Parent {
constructor() {
this.parentFn()
}
parentFn() {
console.log('Parent triggering')
}
}
和孩子:
@Component({
selector: 'child',
template: `...`
})
class Child {
@Input()
fn() {
console.log('triggered from the parent')
}
constructor() {}
}
后台是一种“获取”请求,即从孩子那里获取最新状态。
现在我知道我可以通过服务和 Subject/Observable 实现这一点,但我想知道是否没有更直接的方法?
【问题讨论】:
标签: angular typescript input components output