关于angular2更新时机的一些发现

关于angular2更新时机的一些发现

引入:

angular2官网关于管道(Pipe)的介绍中说明,angular2的管道分为pure和impure。

  • 非纯管道Impure pipes

Angular会在每个组件的变更检测周期中执行非纯管道。 非纯管道可能会被调用很多次,和每个按键或每次鼠标移动一样频繁。

Angular executes an impure pipe during every component change detection cycle. An impure pipe will be called a lot, as often as every keystroke or mouse-move.

  • 纯管道Pure pipes

Angular只有在它检测到输入值发生了纯变更时才会执行纯管道。 纯变更是指对原始类型值(String、Number、Boolean、Symbol)的更改, 或者对对象引用(Date、Array、Function、Object)的更改。

Angular executes a pure pipe only when it detects a pure change to the input value. A pure change is either a change to a primitive input value (String, Number, Boolean, Symbol) or a changed object reference (Date, Array, Function, Object).

Angular会忽略(复合)对象内部的更改。 如果我们更改了输入日期(Date)中的月份、往一个输入数组(Array)中添加新值或者更新了一个输入对象(Object)的属性,Angular都不会调用纯管道。

Angular ignores changes within (composite) objects. It won’t call a pure pipe if we change an input month, add to an input array, or update an input object property.

具体问题:

在以前的开发过程中,我们遇到过,一个双向绑定的数组,使用push或者splice函数时,画面并未发生刷新。目前看了正是由于类似的情况

解决方法:

第一种:

新建一个数组,重新赋值给双向绑定的变量,这样,相当于引用发生了变化,所以会更新模板。

第二种:

使用ngIf来切换模板文件中的代码段(可能是一个组件)。


第一种是方法性能应该更好。如果这种情况下,也有一个类似管道中pure的参数来设置就好了,目前还没发现其他方法。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值