简体   繁体   English

为什么从父组件发出后子组件中的值没有改变?

[英]Why value in child component is not changed after emit from parent?

I have a parent component view-event-component within child component inside:我在子组件内部有一个父组件view-event-component组件:

<app-visitor-component (message)="message($event)" [_event]="event"></app-visitor-component>

Inside view-event-component there is:view-event-component里面有:

  public dataFromChild: any;

  message($event) {
      console.log($event);
      this.dataFromChild = $event;
  }

Template view-event-component is:模板view-event-component是:

{{ dataFromChild }}

So, when I push data from parent to child using @Output I always get message in:因此,当我使用@Output将数据从父级推送到子级时,我总是会收到消息:

message($event) {
   console.log($event);
}

But variable is not showed (rendered) in template:但是变量没有在模板中显示(渲染):

{{ dataFromChild }}

There are no errors and warnings.没有错误和警告。

I use last version of Angular and Mozilla Firefox我使用最新版本的 Angular 和 Mozilla Firefox

If you have:如果你有:

changeDetection: ChangeDetectionStrategy.OnPush

Trigger manually changeDetection:手动触发changeDetection:

constructor(cd: ChangeDetectorRef) {}

triggerDetecton():void {
    this.cd.detectChanges();
}

You also should provide a StackBlitz to help us understand better您还应该提供 StackBlitz 以帮助我们更好地理解

try like this in child:在孩子身上试试这样:

message= new EventEmitter<Object>();
messagefunction(event) {
    this.messageEvent.emit(event);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 子组件初始化后,将值从子组件传递给父组件 - Pass value from Child component to Parent component , after child is initialized 从动态创建的子组件向父组件发出事件 - Emit event from dynamically created child component to parent component 为什么不能正确获取从父组件传递给子组件的值呢? - Why not get the value passed from the parent component to the child component correctly? Angular2:如何在以编程方式从子组件更改但未在父绑定中更改后重置输入属性? - Angular2: How to reset input property after it is changed from child component programatically but not changed in parent binding? 如何从父组件到子组件发射数据[Angular 2] - How to emit data from parent to child component [Angular 2] 如何在 Angular 中从子组件向父组件发出多个事件? - how to emit multiple events from child to parent component in Angular? 如何从子组件获取表单并发送给父组件 - How to get the form from a child component and emit to the parent Eventemitter 不会将数据从子组件发送到父组件 - Eventemitter would not emit the data from child to parent component 发生从父母到孩子的事件 - Emit event from parent to child @Output从指令向父组件Angular 4发出值 - @Output emit value from directive to parent component Angular 4
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM