2017-06-14 145 views
1

這是另一個「View not changing」問題Angular2:View not updating

我有一個組件,當我選擇一個名稱時需要更新。 但是,我不知道爲什麼,我必須在模型更改之前選擇名稱的2倍。這就像我第一次選擇模型沒有更新。

採用了棱角分明2 4.0.0與materializeCSS(自動完成從Materialise公司推出)

Example

那麼,我想直接擁有該按鈕出現一次我選擇。

這部分的HTML模板:

<form> 
      <i class="material-icons prefix">search</i> 
      <input id="search" type="text" name="autoComplete" materialize="autocomplete" [materializeParams]="[autocompleteInit]"> 
     </form> 
     <div *ngIf="CVSelected.getCV()"> 
      <button type="button" [routerLink]="['/dashboard/cv']" class="waves-effect waves-light btn">Passer à l'édition</button> 
     </div> 

AutocompleteInit:

autocompleteInit: any = { 
    data: {Some_data_here}, 
    onAutocomplete: (str: string) => { 
     this.dataService.GetFromString(str).subscribe(value => { 
      console.log(value); 
      this.CVSelected.setCV(value[0]); // This makes the button appears on my template with a get 
     }); 
     this.changement.detectChanges(); // Tried this and other functions that does the same but doesn't work 
    }, 
}; 

任何人都遇到了這個問題?如果是,有些解決方案?

預先感謝您

+0

檢查'的console.log(Zone.current.name)''裏面處理onAutocomplete' – yurzui

+0

找不到有關區圖書館。 current.name。我在哪裏找到它? –

+0

它的zonejs。 '聲明讓Zone:任何' – yurzui

回答

2

在這種情況下最可能的原因是角度區外的執行代碼。爲了解決這個問題,我會建議你到角區域中運行的代碼,如:

constructor(private zone: NgZone) { } 

autocompleteInit: any = { 
    data: {}, 
    onAutocomplete: (str: string) => { 
    zone.run(() => { 
     this.dataService.GetFromString(str).subscribe(value => { 
      console.log(value); 
      this.CVSelected.setCV(value[0]); // This makes the button appears on my template with a get 
     }); 
    }); 
    },