Angular 6 - 文本区域(更改)永远不会更改文本区域的内容
Angular 6 - textarea (change) ever doesn't change the contents of the textarea
我有一个 textarea 问题,如果我修改 textarea 上的内容并且(更改)被触发,它不会通过代码更改 textarea 的内容。
这是一个例子:
app.component.html
<textarea #content (change)="dosomething(content.value)">{{ thecontents | json }}</textarea>
app.component.ts
内容;
dosomething(data) {
// this will overwrite whatever is already in the textarea
this.thecontents = { something : 'someother'};
}
由于某些原因,触发 (change)
时文本区域未更改
为什么?我该如何解决这个问题?
用[value]
或[ngModel]
绑定textarea的内容:
<textarea (change)="dosomething($event.target.value)" [value]="thecontents | json"></textarea>
<textarea (change)="dosomething($event.target.value)" [ngModel]="thecontents | json"></textarea>
有关演示,请参阅 this stackblitz。
如果您不想使用 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;
}
我已经编辑了你的代码,看看这个 https://stackblitz.com/edit/angular-xjksed
我有一个 textarea 问题,如果我修改 textarea 上的内容并且(更改)被触发,它不会通过代码更改 textarea 的内容。
这是一个例子:
app.component.html
<textarea #content (change)="dosomething(content.value)">{{ thecontents | json }}</textarea>
app.component.ts
内容;
dosomething(data) {
// this will overwrite whatever is already in the textarea
this.thecontents = { something : 'someother'};
}
由于某些原因,触发 (change)
时文本区域未更改
为什么?我该如何解决这个问题?
用[value]
或[ngModel]
绑定textarea的内容:
<textarea (change)="dosomething($event.target.value)" [value]="thecontents | json"></textarea>
<textarea (change)="dosomething($event.target.value)" [ngModel]="thecontents | json"></textarea>
有关演示,请参阅 this stackblitz。
如果您不想使用 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;
}
我已经编辑了你的代码,看看这个 https://stackblitz.com/edit/angular-xjksed