[英]Angular - How to get input value at ngFor loop with one way binding
[英]Angular - Detect <input /> value change with one way binding
我有這個
<input [value]="SomeModel.ValueFunct()" readonly="readonly"/>
有用! 函數返回值更改時,顯示的值也會更改。
如何檢測值是否變化?
更新:
使用方法:
ngOnChanges(changes: SimpleChanges) : void{
要么
(gModelChange)="function($event)"
要么
(change)="function($event)"
與
[ngModel]="SomeModel.ValueFunct()"
函數更改時不起作用,僅當用戶更改輸入時才有效(但在這種情況下為只讀)
隨想曲:
http://next.plnkr.co/edit/Rj3NjtcGXTo9upt9?open=lib%2Fapp.ts&deferRun=1&preview 。
。
相同的未解決問題:
因此,不是直接綁定功能,而是將實際值綁定到輸入標簽中。 然后在您的打字稿文件中,編寫ngOnChanges()生命周期事件並檢測此更改。
因此,如果您想要單向數據綁定,那么
<input [ngModel]="variable">
在您的.ts文件中,從angular/core
導入OnChanges
和SimpleChanges
ngOnChanges(changes: SimpleChanges) {
if(changes[variable]) {
// Perform your actions.
}
}
如果要使用一種方法進行數據綁定時使用html輸入字段中更改的數據,則必須使用(ngModelChange),如下所示:
<input [ngModel]="variable"(ngModelChange)="function_to_fire_on_model_change($event)">
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.