angular - 让 mat-autocomplete 不替换但增加值(value)

标签 angular typescript angular-material

我知道使用 mat-autocomplete 的 mat-option 中的 onSelectionChange() 我可以在选择一个选项时做一些事情,但我想要的是自动完成添加的值输入的垫选项而不是替换其内容。

例子:

  • 输入:“你好”
  • 点击垫选项“世界”
  • 输入:“ Hello World ”

如何在不保留表单值的选项卡并存储其先前值并将其放回原处的情况下完成此操作(因为这似乎是一个糟糕的解决方法)?

<mat-form-field class="w-100">
   <textarea [matAutocomplete]="auto" [value]="hello" matInput></textarea>
   <mat-autocomplete #auto="matAutocomplete" >
      <mat-option [value]="world">
         world
      </mat-option>
   </mat-autocomplete>
</mat-form-field>

最佳答案

我的解决方法是始终在选项值前面加上当前输入值

<mat-form-field class="w-100">
   <textarea [matAutocomplete]="auto" [value]="hello" matInput></textarea>
   <mat-autocomplete #auto="matAutocomplete" >
      <mat-option [value]="hello + ' ' + world">  <<=== modified 
         world
      </mat-option>
   </mat-autocomplete>
</mat-form-field>

关于angular - 让 mat-autocomplete 不替换但增加值(value),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54256838/

相关文章:

html - 无法绑定(bind)到 'configuration',因为它不是 'ng-material-multilevel-menu' 的已知属性

angular - 如何本地化我的 Angular 11 库?

ajax - Angular 2 HTTP 进度条

javascript - 如何将 getter 值绑定(bind)到 HTML 中?

javascript - 无论 Angular RxJS 中是否有数据,如何从 Observable.subscribe 调用一次函数?

testing - Ng 测试 - 无法绑定(bind)到 'value',因为它不是 'mat-select' 的已知属性

css - Angular Material CSS 控制台警告

Angular 10 更严格的设置 --strict

html - 输入标签中的#id 是什么意思?

angular - 在 Angular 8 应用程序中实现 session 存储