簡體   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