ngOnChange 不存储 previousValue 属性 [英] ngOnChange not storing previousValue property

查看:27
本文介绍了ngOnChange 不存储 previousValue 属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

[Angular2 @ RC4] [angularfire2 @ 2.0.0-beta.2]

[Angular2 @ RC4] [angularfire2 @ 2.0.0-beta.2]

在我的子组件中,我无法让 changes['posX'].previousValue 存储任何东西.

Inside my child component I cannot get changes['posX'].previousValue to store anything.

来自父 html 的片段:

Snippet from parent html:

//inside *ngfor loop
[posX]='coord.position_x | calculateX:395'
[posY]='coord.position_y | calculateY:380'

来自子组件的片段:

export class childComponent implements OnChanges {
  @Input() posX: number; //updated via firebase database from parant
  @Input() posY: number; //updated via firebase database from parant

  ngOnChanges(changes: {[ propName: string]: SimpleChange}) {
    console.log('Change detected: ', changes['posX'].currentValue);
    console.log('Change detected: ', changes['posX'].previousValue);
  }

Chrome 控制台的结果:

Result from chrome console :

Change detected: posX current =  343 //changes every time
Change detected: posX previous = Object {} //same after updates

我发现了 ngOnChange 的这个 plnkr 并且能够让它在我的应用程序中工作.虽然我无法让 PosX 和 PosY 变量以相同的方式工作.

I found this plnkr of ngOnChange and was able to get it working in my app. Although I'm unable to get the PosX and PosY variables working the same way.

变量 PosX 和 PosY 每 15 秒通过我的 firebase 数据库中的一个可观察值进行更新.知道为什么没有显示 previousValue 吗?

The Variables PosX and PosY are being updated via an observable from my firebase database every 15 seconds. Any idea why previousValue is not showing up?

推荐答案

我能找到的最佳答案是关于 OnChanges() 无法检测到 coord 的更改,因为它是对 coord 对象的引用而不是实际值.

Best answer I could find was about OnChanges() not being able to detect changes to coord because it is a reference to the coord object and not the actual value.

直接引用:

hero 属性的值是对 hero 对象的引用.Angular 并不关心英雄自己的 name 属性是否发生了变化.英雄对象引用没有变化,所以从 Angular 的角度来看,没有变化要报告!

The value of the hero property is the reference to the hero object. Angular doesn't care that the hero's own name property changed. The hero object reference didn't change so, from Angular's perspective, there is no change to report!

链接:https://angular.io/docs/ts/latest/guide/lifecycle-hooks.html#!#onchanges

这篇关于ngOnChange 不存储 previousValue 属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆