2017-04-23 164 views
9

我想從使用自動完成組件的angular2/material2服務器獲取數據。 (https://material.angular.io/components/component/autocompletemd-autocomplete angular2從服務器獲取數據(使用服務)

TS

emailCtrl: FormControl; 
    filteredEmails: any; 

    constructor(
    private companieService: CompanieService, 
) { 
    this.emailCtrl = new FormControl(); 
    this.filteredEmails = this.emailCtrl.valueChanges 
     .startWith(null) 
     .map(email => this.filterEmails(email)); 
    } 


    filterEmails(email: string) { 
    this.userService.getUsersByEmail(email) 
     .subscribe(
     res => { 
      return res 
     }, 
     error => { 
      console.log(error); 
     } 
    ) 
    } 

HTML

<md-input-container> 
     <input mdInput placeholder="Email" [mdAutocomplete]="auto" [formControl]="emailCtrl" [(ngModel)]="fetchedUser.profile.email"> 
    </md-input-container> 

    <md-autocomplete #auto="mdAutocomplete"> 
     <md-option *ngFor="let email of filteredEmails | async" [value]="email"> 
     {{email}} 
     </md-option> 
    </md-autocomplete> 

服務:userService.getUsersByEmail(email)是拉這樣的數據:

['[email protected]','[email protected]','[email protected]'] 

我在自動完成沒有錯誤,但沒有結果。 我看到了鉻(選項卡網絡)的調試數據被正確拉爲輸入中的每個變化

+0

有什麼方法可以讓用戶在輸入框中點擊時顯示所有可用內容,而不是等待輸入第一個字母? –

回答

13

我給你我的例子,我通常使用,

this.SearchForm.controls['city_id'].valueChanges 
    .debounceTime(CONFIG.DEBOUNCE_TIME) 
    .subscribe(name => { 
    this.domain = [['name', 'ilike', name]]; 
    this.DataService.getAutoComplete('res.city', this.domain) 
     .subscribe(res => { 
     return this._filteredCity = res['result']['records'] 
    }) 
    }) 

HTML

<div class="mb-1 ml-1 mt-1" fxFlex="30"> 
      <md-input-container style="width: 100%" > 
      <input mdInput placeholder="Kota" (blur)="checkAutoComplete('city_id')" [mdAutocomplete]="city_id" [required]="true" [formControl]="SearchForm.controls['city_id']"> 
      </md-input-container> 
      <md-autocomplete #city_id="mdAutocomplete" [displayWith]="displayFn"> 
      <md-option *ngFor="let city of _filteredCity" [value]="city"> 
       <span>{{ city.name }}</span> 
      </md-option> 
      </md-autocomplete> 
      <div *ngIf="SearchForm.controls['city_id'].hasError('required') && SearchForm.controls['city_id'].touched" class="mat-text-warn text-sm">Kolom ini wajib diisi.</div> 
     </div> 

就像那樣

+0

有什麼辦法可以讓用戶點擊輸入框而不是等待輸入第一個字母時顯示所有可用的信息? –

2

這就是我所做的。

的.html

 <input formControlName="search" [mdAutocomplete]="auto" type="text" class="form-control"> 
 
<md-autocomplete #auto="mdAutocomplete"> 
 
    <md-option *ngFor="let data of filteredData | async" [value]="data.text"> 
 
    {{ data.text }} 
 
    </md-option> 
 
</md-autocomplete>

.TS

filteredData: Observable<any[]>; // async pipe needs to be an Observable 
myContent: any[] = []; 

this.filteredData = this.myformGroup.get('search').valueChanges 
.debounceTime(400) 
.do(value => { 

    // i don't want to make another request on value change if content placeholder already has it. 
    let exist = this.myContent.findIndex(t => t.text === value); 
    if (exist > -1) return; 

    // get data from the server. my response is an array [{id:1, text:'hello world'}] 
    this.myApiService.getSearch(value).subscribe((res: any[]) => { this.myContent = res; }); 

}).delay(500).map(() => this.myContent); 

讓我知道,如果這對你的作品。

+1

搜索不是住在這個解決方案,結果顯示基於以前的搜索關鍵... – Sreekumar

+0

沒有它以前沒有,因爲如果你想對每個鍵值的請求,刪除這個: 'let exists = this。 myContent.findIndex(t => t.text === value);如果(存在> -1)返回;' – Robin