繁体   English   中英

PIPE 被多次调用,我怎样才能让它只被调用一次?

[英]PIPE is getting called multiple times, how can I make it called only once?

HTML:(component.html)

<input #CostInput class="form-element" formControlName="cost" [value]="replaceDecimal.transform(FormGroup.get('cost').value)">

Pipe.ts:

commaLocaleCodes = ['AM', 'AR'];
transform(input) {
        if (input) {
            const uiLanguage = 'am');
            if (this.commaLocaleCodes.indexOf(uiLanguage.toUpperCase()) > -1) {
                return input.replace(".", ",");
            } else {
                return input.replace(",", ".");
            }
        }
    }

这里的问题是 Pipe 被多次调用。 如果我们发送输入 = 123.45。 pipe 应返回 123,45 并停止。 但是过滤器再次被调用。 再次输入变为 123.45。

Pipe 已经很纯净了。 我想了解我们是否可以在第一次迭代后停止 pipe。 谢谢。

使用默认的更改检测策略,每个更改检测周期都会触发绑定到属性(或指令)的 function。 这就是不鼓励将函数绑定到属性和指令的原因。

而是在 controller 中调用 function,将响应保存到变量并在模板中使用绑定它。

Controller (*.ts)

export class SomeComponent implements OnInit {
  transformedValue: any;

  ngOnInit() {
    this.transformedValue = this.replaceDecimal.transform(FormGroup.get('cost').value);
  }
}

模板 (*.html)

<input #CostInput class="form-element" formControlName="cost" [value]="transformedValue">

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM