<textarea
  matInput
  type="text" class="send-message-input"
  [disabled]="isTypingNotAvailable"
  [rows]="rowsCount"
  placeholder="Type text ..."
  formControlName="text"
  (keydown.control.enter)="onCtrlEnter()"
  (keydown.enter)="onEnter($event)"
  [matAutocomplete]="auto"
></textarea>

当我调用此代码时:

this.sendMessageForm.get('text').setValue('Hi');

我看到给定的文本是如何放置在表单中的,但在文本区(视觉上)却没有显示,为什么?

看那https://stackblitz.com/edit/angular-ivy-plhchb?file=src/app/app.component.html

推荐答案

问题是mat-autocomplete组件上的displayWith属性阻止了用普通字符串以编程方式设置控件值.

提供的displayFn回调函数阻止使用纯字符串设置表单控件.您可以移除displayWith属性,或者更新displayFn以允许设置纯字符串,或者使用对象{value: 'my string'}而不是字符串来设置控件值.

1. Remove the 100 property from the autocomplete component.

//template

<mat-autocomplete 
    #auto="matAutocomplete" 
    [displayWith]="displayFn" <-- remove
>

2. Update the 100 method to allow your manual changes.

// component.ts

  displayFn(template): string {
    return template && template.value ? template.value : template;
  }

一百零二

通过传递对象来设置控制值,例如,control.setValue({value: 'my string'}).

// component.ts

  initForm() {
    this.sendMessageForm = this.fb.group({
      test: [{value: 'Initial text'}],  <-- need to use object {value: 'string'}
    });
  }

  ...

  addText() {
    this.sendMessageForm?.get('test').setValue({value: 'hi'});
  }

Angular相关问答推荐

如何将CDkDropList与Angular FormArray和FormGroup一起使用?

导致无限请求的Angular HttpInterceptor和HttpClientModule

NG构建后多余的国旗

以Angular 将指令事件从子组件传递到父组件

无法绑定到appendTo,因为它不是p-confirmPopup的已知属性

NGNEW不会在src根文件夹ANGLI CLI 17.0.10中生成app.mode.ts

在Angular中使用ngFor进行表乘法

Angular 单调服务已多次创建

Angular 16 Auth Guard在具有0的服务时给出Null Injector错误;httpclient;被注入

区分material 手风琴

Chart.js 如何编辑标题标签 colored颜色

TypeError:this.restoreDialogRef.afterClosed 不是函数

angular 13 ng 构建库失败(ivy部分编译模式)

为什么 Redux 中的对象应该是不可变的?

Angular ngFor 索引从 1 开始 循环

如何在Angular Material2中获取活动选项卡

如何在Angular2中基于特定的构建环境注入不同的服务?

类型错误:无法读取未定义的属性

在 Angular rxjs 中,我什么时候应该使用 `pipe` 与 `map`

如何使用 CLI 创建特定版本的 Angular 项目?