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

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

问题描述

在我的孩子组件里面,我不能得到 changes ['rc2] posX']。previousValue 来存储任何东西。



源代码片段:

  //里面的* ngfor循环
[posX] ='coord.position_x | calculateX:395'
[posY] ='coord.position_y | calculateY:380'

子元素片段:

  export class childComponent implements OnChanges {
@Input()posX:number; //通过parant的firebase数据库更新
@Input()posY:number; //更新来自parant的firebase数据库

ngOnChanges(更改:{[propName:string]:SimpleChange}){
console.log('Change detected:',changes ['posX' ]。当前值);
console.log('Change detected:',changes ['posX']。previousValue);

$ / code $ / pre
$ b $ p

结果来自chrome控制台:

 检测到更改:posX current = 343 //每次更改
检测到更改:posX previous = Object {} //在更新
之后相同c $ c>

我发现这个 plnkr ,并能够在我的应用程序中工作。尽管我无法使PosX和PosY变量以相同的方式工作。

变量PosX和PosY正在通过每15秒我的firebase数据库中的一个可观察值进行更新。任何想法为什么previousValue没有显示?

解决方案

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

直接引用:


英雄属性的值是对英雄对象的引用。 Angular不关心英雄自己的名字属性改变了。英雄对象引用并没有改变,所以从Angular的角度来说,没有任何改变可以报告!


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


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

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

Snippet from parent html:

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

Snippet from Child component:

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);
  }

Result from chrome console :

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

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.

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?

解决方案

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.

Direct Quote:

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!

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

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

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