【问题标题】:Angular 2 - displaying results after http request (Service)Angular 2 - http请求后显示结果(服务)
【发布时间】:2016-03-10 11:11:03
【问题描述】:

我正在尝试在 ngFor 中迭代我的结果 - 它不像 Angular 1 那样直观。我有以下内容:

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) {}
}

一个典型的json结果:

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

}

发生了什么?好吧,{{searchSvc | async | json}} 给我看结果。 但是ngFor:没有:(

有什么线索吗?提前谢谢你!!!

【问题讨论】:

标签: typescript angular angular2-services angular2-http


【解决方案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);
}

编辑

如果你想从 observable 接收完整的对象,我看到了两种方法:

  • 显式订阅:

    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">
    

【讨论】:

  • 您好,Therry ;) 好吧..我编辑了结果...实际上它可以返回房间或颜色。如果我在服务上设置它..我会限制自己只是抓取房间数组:(只是出于好奇,我以这种方式设置来测试你在 ngFor 上的异步并且它有效..但我真的需要迭代不同的键....
  • 快到了!我刚刚添加: import "Pipe, PipeTransform} from "angular2/core" ,@component....pipes: [FieldPipe]....@ pipe(....) ,@export class FieldPipe (.... ) 这仍然给我一个在 TypeaheadComponent 上找到的 No Directive 注释 - 缺少什么?
  • 你的TypeaheadComponent组件上好像少了一个@Component注解...能不能给我对应的代码?
  • 确定! [typeahead]:从'../../_generics/field-pipe.component'导入{FieldPipe};______@Component({选择器:“typeahead”,提供者:[SearchService],管道:[FieldPipe] ...模板:&lt;div class="typeaheadRowRooms" *ngFor="#item of searchSvc | async | field:rooms"&gt;&lt;div class="typeaheadRoomTitle"&gt;{{item.name}}&lt;/div&gt;&lt;/div&gt; ____ 导出类 TypeaheadComponent (...) this.searchSvc = this._searchService.DoGeneralSearch(search).subscribe( (data) => { this.searchSvc = data; } ); [field-pipe-component ] : 导入和@pipe 和导出
  • 我现在把你的管道放在了一个不同的文件中......现在它把我扔了 例外:在 [searchSvc |异步 |字段:TypeaheadComponent 中的房间@15:43]
猜你喜欢
  • 1970-01-01
  • 2018-03-16
  • 1970-01-01
  • 1970-01-01
  • 2017-02-10
  • 1970-01-01
  • 1970-01-01
  • 2011-03-05
  • 1970-01-01
相关资源
最近更新 更多