在Angular中的mat-autocomplete中进行字符串替换。

huangapple go评论69阅读模式
英文:

String replace in mat-autocomplete in Angular

问题

我是初学者的Web开发者。我有以下代码:

<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>

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

请帮助我。

英文:

I am beginner webdeveloper. I have this code:

<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, '');
}

and it works fine.

Now I need to add this function to 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>

And it doesn't work.
How can I fix it?

Please help me

答案1

得分: 2

你可以将自动完成与你自己的输入连接起来,然后像对常规输入一样操纵输入值:

<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>

在 stackblitz 中查看工作示例

英文:

You can connect autocomplete to your own input and then manipulate the input value as you did to the regular input:

&lt;input
  matInput
  [(ngModel)]=&quot;address.city&quot;
  [matAutocomplete]=&quot;auto&quot;
  (keydown)=&quot;removeSpecialCharactersFromCity($event)&quot;
  (keyup)=&quot;removeSpecialCharactersFromCity($event)&quot;&gt;

&lt;mat-autocomplete autoActiveFirstOption #auto=&quot;matAutocomplete&quot;&gt;
  &lt;mat-option *ngFor=&quot;let option of cityFilteredOptions&quot; [value]=&quot;option&quot;&gt;
    {{option}}
  &lt;/mat-option&gt;
&lt;/mat-autocomplete&gt;

Working example in stackblitz

答案2

得分: 0

更改mat-autocomplete上的(keydown)和(keyup)事件绑定为(input)事件绑定。

<mat-autocomplete autoActiveFirstOption #auto="matAutocomplete" (input)="removeSpecialCharactersFromCity($event)">
  <mat-option *ngFor="let option of cityFilteredOptions" [value]="option">
    {{option}}
  </mat-option>
</mat-autocomplete>

更新removeSpecialCharactersFromCity函数以直接使用事件对象。

removeSpecialCharactersFromCity(event: any) {
  this.adres.city = event.target.value.replace(/[&\/\\#,+()$~%.\]'":*?<>{}@^&=;'"_!\[]/g, '');
}

编辑:

(input)="removeSpecialCharactersFromCity($event)"

更改为

(input)="removeSpecialCharactersFromCity($event.target.value)"
英文:

Change the (keydown) and (keyup) event bindings on mat-autocomplete to (input) event binding.

&lt;mat-autocomplete autoActiveFirstOption #auto=&quot;matAutocomplete&quot; (input)=&quot;removeSpecialCharactersFromCity($event)&quot;&gt;
  &lt;mat-option *ngFor=&quot;let option of cityFilteredOptions&quot; [value]=&quot;option&quot;&gt;
    {{option}}
  &lt;/mat-option&gt;
&lt;/mat-autocomplete&gt;

Update the removeSpecialCharactersFromCity function to use the event object directly.

removeSpecialCharactersFromCity(event: any) {
  this.adres.city = event.target.value.replace(/[&amp;\/\\#,+()$~%.\]&#39;&quot;:*?&lt;&gt;{}@^&amp;=;&#39;&quot;_!\[]/g, &#39;&#39;);
}

Edit:

Change

(input)=&quot;removeSpecialCharactersFromCity($event)&quot;

to

(input)=&quot;removeSpecialCharactersFromCity($event.target.value)

huangapple
  • 本文由 发表于 2023年7月3日 15:57:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76602831.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定