角度:未检测到管道变化 [英] Angular: Change in Pipe not detected

查看:71
本文介绍了角度:未检测到管道变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个管道,它将 input 值乘以从服务检索到的另一个值:

I have this pipe which multiplies the input value by an other value retrieved from a service:

@Pipe({
    name: 'multiply'
})
export class MultiplyPipe implements PipeTransform {
    constructor(private service: StateService) { }

    transform(value: any, args?: any): any {
        return value * this.service.multiplier;
    }
}

用法:

{{ value | multiply }}

演示

这很好用,但是当服务中的multiply值被更改时,它不会触发任何更改检测,因此

This works fine, but when the multiply value from the service is changed, it doesn't trigger any change detection, and thus

 {{ value | multiply }}

不会再次运行,将旧值保留在屏幕上.有什么建议可以解决吗?

is not run again, leaving the old value on the screen. Any suggestion how this can be fixed?

推荐答案

Angular文档,如此stackblitz 所示,这是强制实施的一种方法要调用的管道是不纯的:

As discussed in Angular documentation, and as shown in this stackblitz, one way to force the pipe to be called is to make it impure:

@Pipe({
  name: 'multiply',
  pure: false
})

有关纯净管道和不纯净管道的更多详细信息,请参见

For more details about pure and impure pipes, you can see this article.

这篇关于角度:未检测到管道变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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