2017-03-29 45 views
0

観測可能なコレクションに結合できないNGXデータテーブル6.3を使用しています。 Angular Observable Collectionを通常の観測不可能な配列に変換するにはどうすればよいですか?ここで変換角度2観測不可観測不可

は、私がサービスのために持っているものである:ここでは

private _productUrl = '../../api/todos/todos.json'; 

constructor(private _http: Http) { } 

getToDos(): Observable<IToDo[]> { 
    return this._http.get(this._productUrl) 
     .map((response: Response) => <IToDo[]> response.json()) 
     .do(data => console.log('All: ' + JSON.stringify(data))) 
     .catch(this.handleError); 
} 

は、私がcomponent.tsのために持っているものです。それは私にforeachループ上のエラーを与える:は、プロパティを読み取ることができません未定義

ngOnInit() { 
    this._toDoService.getToDos() 
     .subscribe(
      toDos => this.toDos = toDos, 
      error => this.errorMessage = <any>error); 

    this.rows = this.transform(this.toDos); 
} 

transform(source: IToDo[]): IToDo[] { 
    let dest: IToDo[] = []; 

    for (let sourceItem of source) 
    { 
     let destItem: IToDo = { 
     toDoId: sourceItem.toDoId, 
     name: sourceItem.name, 
     priority: sourceItem.priority, 
     dueDate: sourceItem.dueDate, 
     completed: sourceItem.completed 
     }; 
     dest.push(destItem); 
    } 

    return dest; 
} 

答えて

2

観測の「長さ」は非同期であるので、サブスクリプションは、あなたが作る呼び出した後、あなたは、単に右トドス操作傾けます。代わりに、サブスクリプションの内側にドスを操作:

ngOnInit() { 
    this._toDoService.getToDos() 
    .subscribe(
     toDos => { 
     this.toDos = toDos 
     this.rows = this.transform(this.toDos); 
     }, 
     error => this.errorMessage = <any>error); 
} 

あなたthis.transformコールが同期的に呼び出されていた、あなたのコールスタックが呼び出しの単なる線形進行であることを意味。

0

asyncパイプを使用して、Observable入力をアンラップすることもできます。

<ngx-datatable 
     class="material" 
     [rows]="todos | async" 
     ...... 
</ngx-datatable> 
+0

ダビデ、親切な提案に感謝します。私はまずそれを試しましたが、何らかの理由でそれがバインディングで動作しませんでした。 https://github.com/swimlane/ngx-datatable/issues/373 –

関連する問題