[英]How to check for changes with pipes? Angular text area
I have a text area with formatted json. 我有一个带有json格式的文本区域。 the user is allowed to make changes in that text area.
允许用户在该文本区域中进行更改。 However because of the json pipe I can't use
[(ngmodel)}
. 但是由于json管道,我不能使用
[(ngmodel)}
。 Also (change)
and (ngModelChange)
don't seem to trigger anything. 而且
(change)
和(ngModelChange)
似乎什么也不会触发。 How do I capture the user changes? 如何捕获用户更改?
data: string = '{"a":1,"b":2,"c":{"d":3, "e":4}}';
ngOninit(){
this.data= JSON.parse(this.data);
}
saveUserChanges(){
console.log(this.data)
}
HTML HTML
<text area (ngModelChange)="saveUserChanges()">{{data | json}}</textarea>
<button (click)="saveUserChanges()">save</button>
You can bind the value with [ngModel]
and set the new value with (ngModelChange)
: 您可以使用
[ngModel]
绑定值,并使用(ngModelChange)
设置新值:
<textarea [ngModel]="data | json" (ngModelChange)="saveUserChanges($event)"></textarea>
In the component class, saveUserChanges
is defined as: 在组件类中,
saveUserChanges
定义为:
saveUserChanges(value) {
this.data = JSON.parse(value);
}
See this stackblitz for a demo. 有关演示,请参见此堆叠闪电战 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.