2016-03-10 21 views
0

我想重複我的結果我ngFor - 它不是那麼直觀像角1.我以下幾點:角2 - HTTP請求(服務)後,顯示結果

search.service.ts

import {Injectable} from 'angular2/core'; 
import {Http, HTTP_PROVIDERS, Response} from 'angular2/http'; 
import 'rxjs/add/operator/map'; 

@Injectable() 
export class SearchService { 

    constructor(private _http: Http) { 

    } 

    DoGeneralSearch(s){ 
    return this._http.get('http://localhost:6000/search?q=' + s) 
    .map((res:Response) => res.json()) 
    } 
} 

typeahead.ts

import {Component, Input, OnChanges} from "angular2/core"; 
import {SearchService} from "../../../services/search.service"; 

@Component({ 
    selector: "typeahead", 
    providers: [SearchService], 
    template : ` 
    <div>{{searchSvc | async | json}}</div> 
    <div id="typeaheadRooms" *ngIf="searchSvc"> 
     <div class="typeaheadRowRooms" *ngFor="#item of searchSvc?.rooms"> 
     <div class="typeaheadRoomTitle">{{item.name}}}</div> 
     </div> 
     <div class="typeaheadRowRooms" *ngFor="#item of searchSvc?.colors"> 
     <div class="typeaheadRoomTitle">{{item.name}}}</div> 
     </div> 
`, 
}) 
export class TypeaheadComponent implements OnChanges { 

    @Input() txt: string; 
    display = false; 

    ngOnChanges(changes: {[propName: string]: SimpleChange}) { 
    var search = changes['txt'].currentValue; 
    if(search.length > 2) { 
     this.display = true; 
     this.searchSvc = this._searchService.DoGeneralSearch(search); 
    } 
    else 
    { 
     this.display = false; 
    } 
    } 

    constructor(private _searchService: SearchService) {} 
} 

在JS的典型結果於:

{ 
"max":20, 
"queryString":"chem", 
"rooms":[ 
    { 
    "name":"Lima" 
    }, 
    { 
    "name":"Delta" 
    } 
], 
"colors":[ 
    { 
    "name":"Red" 
    }, 
    { 
    "name":"Lime" 
    } 
] 

}

發生了什麼事?那麼,{{searchSvc | async | json}}顯示我的結果。 但ngFor:無:(

任何線索預先感謝您!

+0

可能的複製[通過在可觀測角JS JSON字符串迭代2](http://stackoverflow.com/questions/35339411/iterate-through-json- string-in-observable-angular-js-2) –

+0

不,Gunter ...不適用於我的情況。但是,謝謝,因爲我收藏了這篇文章 - 將來會非常有用 –

回答

1

在這種情況下,你需要使用這樣的:

<div class="typeaheadRowRooms" *ngFor="#item of searchSvc | async"> 

,並更新你的服務是這樣的:

DoGeneralSearch(s){ 
    return this._http.get('http://localhost:6000/search?q=' + s) 
    .map((res:Response) => res.json().rooms); 
} 

編輯

如果你想從可觀察收到完整的對象,我看到了兩種方法來做到這一點:

  • 明確訂閱:

    this._searchService.DoGeneralSearch(search).subscribe(
        (data) => { 
        this.searchSvc = data; 
        } 
    ); 
    

    ,並在模板

    <div class="typeaheadRowRooms" *ngFor="#item of searchSvc?.rooms"> 
    
  • 定製管道以獲得rooms數據:

    @Pipe({name: 'field'}) 
    export class FieldPipe implements PipeTransform { 
        transform(value, args:string[]) : any { 
        return value[args[0]]; 
        } 
    } 
    

    和在模板

    <div class="typeaheadRowRooms" *ngFor="#item of searchSvc | async | field:rooms"> 
    
+0

嗨,再次,泰瑞;)呃..我編輯了結果...它實際上可以返回房間或顏色。如果我在服務上設置這個..我會限制自己只是爲了搶房間數組:(只是爲了好奇,我設置了這種方式來測試你在ngFor上的異步,它的工作原理..但我真的需要迭代不同的鍵.... –

+0

幾乎就在那裏!我剛剛添加了:從「angular2/core」,@ component ....導入「Pipe,PipeTransform}」管道:[FieldPipe] .... @ pipe(....),@導出類FieldPipe(....),這仍然讓我在TypeaheadComponent上找到No Directive註釋 - 丟失了一些東西? –

+0

看起來'TypeaheadComponent'組件上缺少一個'@ Component'註釋...你能否給我相應的代碼? –