typescript Angular 6 - textarea (change) 永远不会改变 textarea 的内容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/50968003/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Angular 6 - textarea (change) ever doesn't change the contents of the textarea
提问by ConnorsFan
I am having an isse with a textarea where if I modify the contents on the textarea and the (change) is trigger it won't change the contents of the textarea by code.
我有一个 textarea 问题,如果我修改 textarea 上的内容并且(更改)被触发,它不会通过代码更改 textarea 的内容。
Here is an example:
下面是一个例子:
app.component.html
应用程序组件.html
<textarea #content (change)="dosomething(content.value)">{{ thecontents | json }}</textarea>
app.component.ts
app.component.ts
thecontents;
内容;
dosomething(data) {
// this will overwrite whatever is already in the textarea
this.thecontents = { something : 'someother'};
}
For some reason the textarea is not being changed when the (change)
is triggered
由于某种原因,文本区域在(change)
触发时没有改变
Why? How can I fix this?
为什么?我怎样才能解决这个问题?
采纳答案by Chellappan ?
If you don't want to ngModel you can use View Child Directive to achive the same result
如果您不想使用 ngModel,您可以使用 View Child Directive 来获得相同的结果
@ViewChild('content') con:ElementRef;
thecontents={something:''};
name='';
dosomething(data) {
// this will overwrite whatever is already in the textarea
this.con.nativeElement.value=1;
}
I have edited your code check this out https://stackblitz.com/edit/angular-xjksed
我已经编辑了您的代码,请查看https://stackblitz.com/edit/angular-xjksed
回答by ConnorsFan
Bind the content of the textarea with [value]
or with [ngModel]
:
用[value]
或绑定 textarea 的内容[ngModel]
:
<textarea (change)="dosomething($event.target.value)" [value]="thecontents | json"></textarea>
<textarea (change)="dosomething($event.target.value)" [ngModel]="thecontents | json"></textarea>
See this stackblitzfor a demo.
有关演示,请参阅此 stackblitz。