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
2
Answers
Change the (keydown) and (keyup) event bindings on mat-autocomplete to (input) event binding.
Update the removeSpecialCharactersFromCity function to use the event object directly.
Edit:
Change
to
You can connect autocomplete to your own input and then manipulate the input value as you did to the regular input:
Working example in stackblitz