angular 6 - ngOnChanges不会触发嵌套数组的变化
创始人
2024-10-16 01:30:47
0

在Angular 6中,ngOnChanges不会自动检测嵌套数组的变化。但你可以使用另外的方法来触发嵌套数组的变化。

以下是一个解决方法的示例代码:

在父组件中,你可以创建一个新的属性来存储嵌套数组的变化,并在ngOnChanges方法中检测这个属性的变化。然后,你可以在子组件中使用@Input装饰器来监听这个新属性的变化。

父组件示例代码:

import { Component, OnChanges, SimpleChanges } from '@angular/core';

@Component({
  selector: 'app-parent',
  template: `
    
  `
})
export class ParentComponent implements OnChanges {
  nestedArray: any[] = [];

  ngOnChanges(changes: SimpleChanges) {
    if (changes['nestedArray']) {
      this.nestedArrayChange(changes['nestedArray'].currentValue);
    }
  }

  nestedArrayChange(value: any[]) {
    // 处理嵌套数组的变化
    console.log('嵌套数组发生了变化:', value);
  }
}

子组件示例代码:

import { Component, Input, OnChanges, SimpleChanges } from '@angular/core';

@Component({
  selector: 'app-child',
  template: `
    
  `
})
export class ChildComponent implements OnChanges {
  @Input() nestedArray: any[];

  ngOnChanges(changes: SimpleChanges) {
    if (changes['nestedArray']) {
      this.nestedArrayChange(changes['nestedArray'].currentValue);
    }
  }

  nestedArrayChange(value: any[]) {
    // 处理嵌套数组的变化
    console.log('嵌套数组发生了变化:', value);
  }
}

使用这种方法,当父组件的嵌套数组发生变化时,会调用嵌套数组的变化处理方法,然后再将新的嵌套数组的值传递给子组件进行处理。

相关内容

热门资讯

总算了解!wepoker透视苹... 总算了解!wepoker透视苹果系统,约局吧开挂神器是真的吗,必备教程(有挂方法);最新版2024是...
推荐十款!aapoker透视插... 1、推荐十款!aapoker透视插件,德州私人局怎么透视,AI教程(有挂攻略);详细教程。2、德州私...
科技介绍!pokemmo辅助官... 1、科技介绍!pokemmo辅助官网,约局吧是否有挂,透牌教程(有挂透视)2、进入游戏-大厅左侧-新...
总算了解!大菠萝手游辅助,we... 总算了解!大菠萝手游辅助,wepoker破解器,辅助教程(有挂技巧);玩家必备必赢加哟《136704...
推荐十款!hhpoker到底可... 推荐十款!hhpoker到底可以作弊吗,impoker辅助,教你教程(有挂软件)是由北京得hhpok...
重磅来袭!cloudpoker... 重磅来袭!cloudpoker作弊,hhpoker德州作弊,2025新版教程(有挂技巧)准备好在hh...
避坑细节!佛手在线大菠萝辅助,... 避坑细节!佛手在线大菠萝辅助,hhpoker到底可以作弊吗,解密教程(有挂辅助);科技安装教程;13...
重大通报!hhpoker外挂靠... 重大通报!hhpoker外挂靠谱吗,wepoker辅助透视,可靠技巧(有挂透视);亲真的是有正版授权...
来一盘!德州局怎么透视,智星菠... 1、来一盘!德州局怎么透视,智星菠萝透视,可靠教程(有挂方法)。2、德州局怎么透视透视辅助简单,德州...
交流学习经验!wepoker可... 交流学习经验!wepoker可以透视码,wepoker辅助器软件下载,wepoke教程(有挂教程);...