Angular 角度子函数调用父函数,然后父函数将数据传递给子函数

Angular 角度子函数调用父函数,然后父函数将数据传递给子函数,angular,input,output,parent-child,eventemitter,Angular,Input,Output,Parent Child,Eventemitter,我想做的是,若子组件调用父组件的函数,父组件将向子组件传递一些数据。 但现在的问题与本例类似,即使父函数被调用了5次,由于传递的数据没有变化,子函数也无法检测到。 我能做到的是,无论来自家长的数据是否被修改,孩子都可以从家长那里得到通知,并拥有正确的数据 Child.ts @Input() set dataFromParent(v:any){ console.log(v) } @Output() callParentFunction = new EventEmitter<any&g

我想做的是,若子组件调用父组件的函数,父组件将向子组件传递一些数据。 但现在的问题与本例类似,即使父函数被调用了5次,由于传递的数据没有变化,子函数也无法检测到。 我能做到的是,无论来自家长的数据是否被修改,孩子都可以从家长那里得到通知,并拥有正确的数据

Child.ts

@Input() set dataFromParent(v:any){
    console.log(v)
}
@Output() callParentFunction = new EventEmitter<any>()
ngOnInit() {
    for(let i = 0; i< 5;i++){
        this.callParentFunction.emit()
    }
}

您的子组件在几毫秒内调用了5次,最后我们可以看到最终的输出only@Gopal否,子组件未被调用5次,因为传递的变量未被修改,并且不会触发set函数
<app-child (callParentFunction)="test()" [dataFromParent]="isData"/>
isData = true
test=()=>{
    this.isData = true
}