【问题标题】:Angular 6 - textarea (change) ever doesn't change the contents of the textareaAngular 6 - textarea(更改)永远不会更改 textarea 的内容
【发布时间】:2018-11-30 18:07:03
【问题描述】:

我对 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) 被触发时,文本区域没有被更改

为什么?我该如何解决这个问题?

【问题讨论】:

  • 尝试(keyup)而不是(更改)。

标签: angular typescript angular6


【解决方案1】:

[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 以获取演示。

【讨论】:

    【解决方案2】:

    如果您不想使用 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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-17
      • 2014-03-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多