typescript Angular 5 从子组件更新父组件值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48618100/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Angular 5 Update Parent Component value from child Component
提问by Developer
Child Component TS
子组件 TS
import { Component, OnInit, Input, Output } from '@angular/core';
import { EventEmitter } from 'events';
export class ChildComponent implements OnInit {
@Output() OpenScheduleCall = new EventEmitter<boolean>();
onLog() {
this.OpenScheduleCall.emit(false);
}
}
parent Component HTML :
父组件 HTML :
<div [(hidden)]="OpenScheduleCall">
// content
</div>
<app-schedule-call *ngIf="!!OpenScheduleCall" [prospectid]='prospectid' [(OpenScheduleCall)]="OpenScheduleCall"></app-schedule-call>
I am setting the values in child component but changes are not reflecting in parent component
我正在设置子组件中的值,但更改未反映在父组件中
采纳答案by Suren Srapyan
Just Output
cannot be in two-waydata binding. Add also ()
at the end of the bounded function.
就是Output
不能在双向数据绑定。也在()
有界函数的末尾添加。
(OpenScheduleCall)="YourFunctionInParent($event)"
回答by AJT82
You have not marked OpenScheduleCall
as an input to the child component, so first of all you need to do that. And to achieve two-way-binding with banana in the box, your @Output
needs to be the @Input
variable name, with the suffix Change
. So first mark the variable OpenScheduleCall
as @Input
to child and then change the name for @Output
variable:
您尚未标记OpenScheduleCall
为子组件的输入,因此首先您需要这样做。并且要实现与盒子中的banana 的双向绑定,你@Output
需要是@Input
变量名,后缀为Change
. 因此,首先将变量标记OpenScheduleCall
为@Input
child,然后更改@Output
变量的名称:
export class ChildComponent implements OnInit {
@Input() OpenScheduleCall;
@Output() OpenScheduleCallChange = new EventEmitter<boolean>();
onLog() {
this.OpenScheduleCallChange.emit(false);
}
}
Now you have two-way-binding:
现在你有双向绑定:
[(OpenScheduleCall)]="OpenScheduleCall"