javascript - Angular 中 mat-autocomplete 中的字符串替换

标签 javascript angular

我是初学者网络开发人员。我有这个代码:

<input id="street{{sid}}" 
       name="street{{sid}}" 
       type="text" 
       placeholder="" 
       class="form-control"
       [(ngModel)]="adres.street" 
       #street="ngModel" 
       required="" 
       (keydown)="this.removeSpecialCharactersFromStreet($event)"
       (keyup)="this.removeSpecialCharactersFromStreet($event)"
       capitalize-first="true">
removeSpecialCharactersFromStreet(event) {
  this.adres.street = event.target.value.replace(/[&\/\\#,+()$~%.\]'":*?<>{}@^&=;'"_!\[]/g, '');
}

而且效果很好。

现在我需要将此函数添加到 mat-autocomplete 中:

removeSpecialCharactersFromCity(event) {
  this.adres.city = event.target.value.replace(/[&\/\\#,+()$~%.\]'":*?<>{}@^&=;'"_!\[]/g, '');
}
<mat-autocomplete 
       autoActiveFirstOption 
       #auto="matAutocomplete"
       (keydown)="this.removeSpecialCharactersFromCity($event)" 
       (keyup)="this.removeSpecialCharactersFromCity($event)">
  <mat-option *ngFor="let option of cityFilteredOptions" 
              [value]="option"
              (keydown)="this.removeSpecialCharactersFromCity($event)" 
              (keyup)="this.removeSpecialCharactersFromCity($event)">
    {{option}}
  </mat-option>
</mat-autocomplete>

而且它不起作用。 我该如何修复它?

请帮助我

最佳答案

您可以将自动完成功能连接到您自己的输入,然后像处理常规输入一样操作输入值:

<input
  matInput
  [(ngModel)]="address.city"
  [matAutocomplete]="auto"
  (keydown)="removeSpecialCharactersFromCity($event)"
  (keyup)="removeSpecialCharactersFromCity($event)">

<mat-autocomplete autoActiveFirstOption #auto="matAutocomplete">
  <mat-option *ngFor="let option of cityFilteredOptions" [value]="option">
    {{option}}
  </mat-option>
</mat-autocomplete>

Working example in stackblitz

关于javascript - Angular 中 mat-autocomplete 中的字符串替换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76602831/

相关文章:

javascript - 如何将用户ID从浏览器传递到 Protractor ?

javascript - Angular 2 本地存储

jquery - 获取 Angular 2 中的文档高度

angular - highcharts- Angular : Cannot read property 'chart' of undefined

css - 无法将元素粘在垫片内

javascript - 包裹本地主机服务器未更新

javascript - 匹配两个日期,如果匹配则返回 true

javascript - 正则表达式过滤掉除特定日期之外的所有内容

javascript - 图像路径在我的 Angular 4 文件中不起作用

javascript - 使用 Javascript 使用 Button 垂直居中 Div