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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 05:12:14  来源:igfitidea点击:

Angular 5 Update Parent Component value from child Component

angulartypescriptangular-components

提问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 Outputcannot be in two-waydata binding. Add also ()at the end of the bounded function.

就是Output不能在双向数据绑定。也在()有界函数的末尾添加。

(OpenScheduleCall)="YourFunctionInParent($event)"

回答by AJT82

You have not marked OpenScheduleCallas 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 @Outputneeds to be the @Inputvariable name, with the suffix Change. So first mark the variable OpenScheduleCallas @Inputto child and then change the name for @Outputvariable:

您尚未标记OpenScheduleCall为子组件的输入,因此首先您需要这样做。并且要实现与盒子中的banana 的双向绑定,你@Output需要是@Input变量名,后缀为Change. 因此,首先将变量标记OpenScheduleCall@Inputchild,然后更改@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"