简体   繁体   English

Angular 如何通过以下方式检测从父母到孩子的变化

[英]Angular how to detect changes from parent to child in the following way

Sample Component has data which array of object and it has child component on their array loop.示例组件的数据为 object 数组,并且其数组循环中有子组件。 Sample.component样本.组件

export class SampleComponent implements OnInit {
  data = [{ value: 3 }, { value: 1 }];
  constructor() {}

  ngOnInit(): void {}
  valueChange() {
    this.data[1].value = 5;
    this.data = [...this.data];
  }
  whenComponentRerendered() {
    console.log('Parent component rerendered');
  }
  consoleOutput() {
    console.log('Console from button click');
  }
}
<button (click)="valueChange()">
  Change input to 5
</button>
<ng-container *ngFor="let item of data">
  <app-samplechild [data]="item"></app-samplechild>
</ng-container>

now I need to detect changes when any value on object is changed from parent现在我需要检测 object 上的任何值从父级更改时的更改

export class SamplechildComponent implements OnInit {
  @Input('data') data!: any;
  constructor() {}

  ngOnInit(): void {}
  whenComponentRerendered() {
    console.log('child component rerendered');
  }
  changeValue() {
    this.data.value = 5;
  }
}
<p>
  The value from parent is
  {{ data | json }}
</p>

I couldn't get the changes since the data is not an Input value.我无法获得更改,因为数据不是输入值。 How to get changes in child?如何获得孩子的变化? StackBlitz Note: This is only sample code, in realistic data has huge number arrays of object with multiple hierarchy. StackBlitz注意:这只是示例代码,在现实数据中有大量 arrays 的 object 具有多个层次结构。

Remove changeDetection: ChangeDetectionStrategy.OnPush from SamplechildComponent .从 SamplechildComponent 中移除changeDetection: ChangeDetectionStrategy.OnPush SamplechildComponent Then it works fine.然后它工作正常。 You should use default angular changeDetection system.您应该使用默认的 angular changeDetection系统。

暂无
暂无

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

相关问题 如何根据基础组件的变化检测子组件的变化 - Angular - How to detect changes in child component with respect to changes in the base component - Angular 如何从父级更改子级组件? - How to make changes in child component from parent? 如何在Angular4中检测对父div而不是子div的聚焦 - How to detect focusout on parent div not on child div in Angular4 有没有办法从父控制器向角度控制器发送一个承诺? - Is there a way to send a promise from parent controller to child controller in angular? 从子页面JQuery检测对父页面中的隐藏字段所做的更改 - Detect changes made to a hidden field in the parent page from child page JQuery Ionic 5 - 从子页面传递到父页面后,我需要检测对数组的更改 - Ionic 5 - I need to detect changes to an array after passing from a child page to the parent 从父对象到子对象传递复杂的对象。 父级有什么方法可以检测输出何时更改? - Passing around complex objects from parent to child. Any way for parent to detect when its output changed? 当变量使用Angular基于更改事件更改时,将变量从子组件传递到父组件 - pass variable from child to parent component when variable changes based on a change event using Angular AngularJS-绑定到父级以触发子级的角度方式 - AngularJS - Angular way of binding on parent to trigger child 如何以角度为父母和孩子制作动画? - How to animate parent and child in angular?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM