재료 자동 완성에서 옵션을 선택할 때마다 서버에 추가 HTTP GET 호출이 이루어집니다. 드롭 다운 옵션을 선택하면 입력을 채우기 만하면됩니다.각도 재질 드롭 다운 옵션을 선택한 후 추가 시간이 자동으로 표시됩니다.
드롭 다운 옵션은 서버에서 동적으로 검색됩니다.
HTML :
<form class="example-form">
<mat-form-field class="example-full-width">
<input type="text" placeholder="search item" aria-label="Item1" matInput [formControl]="searchTerm" [matAutocomplete]="auto1">
<mat-autocomplete #auto1="matAutocomplete" [displayWith]="displayFn">
<mat-option *ngFor="let item of searchResult" [value]="item.Id">
{{ item.Name }}
</mat-option>
</mat-autocomplete>
</mat-form-field>
</form>
타이프 라이터는 : 다른
searchTerm : FormControl = new FormControl();
searchResult = [];
constructor(private service: AppService){
this.searchTerm.valueChanges
.debounceTime(400)
.subscribe(data => {
this.service.search_word(data).subscribe(response =>{
this.searchResult = response
})
모든 것이 잘 작동합니다. 유일한 문제는 자동 완성 옵션이 선택되었을 때 서버에 대한 예기치 않은 추가 호출입니다.
빠른 업데이트 : 답변이 아직 완전히 해결되지 않았습니다. 그러나, 나는 이것을 displayWith에 문제가 될 수있는 범위로 좁혔다.
이유가 무엇인가요? 예기치 않은? 그것은 가치가 그것을 변경하지? –
당신은'valueChanges'을 구독했습니다. 옵션을 선택하면 텍스트 필드의 값이 변경된다는 잘못된 점이 있습니까? '.distinctUntilChanged()'를 추가 할 수 있습니다. –
제가 받고 있습니다 : [ 'ts]'distinctUntilChanged '속성이'FormControl '유형에 존재하지 않습니다. – Chemdream