【问题标题】:subscribe in Angular 2订阅 Angular 2
【发布时间】:2016-10-09 19:08:36
【问题描述】:

我希望我的 ngOnInit 函数执行以下操作: - 使用 this.structureRequest.sendRequest() 对一些数据进行 http 请求,它工作正常,并且在收到数据后使用 this.viewNodes() 函数开始查看它。 我使用 subscribe ,但它不起作用,我认为我在 subscribe 功能上做错了。请帮忙:)

  1. HomeComponent.ts

    import {Component} from '@angular/core';
    import {Observable} from 'rxjs/Observable';
    import {StructureRequestService} from './StructureRequestService';
    
    
    export class Content {
    
    
    ok: boolean;
    content = [];
    }
    @Component({
    providers: [StructureRequestService],
    styleUrls: ['app/home/home.css'],
    templateUrl:'./app/home/homePageTemplate.html'
    
    })
    
    export class HomeComponent {
    contentArray = [];
    myRes: Content;
    showAssigned:boolean = false;
    showSubitems:boolean = false;
    showUsers:boolean = false;
    
    constructor(private structureRequest: StructureRequestService) {}
    
    ngOnInit() {
    this.structureRequest.sendRequest().subscribe( this.viewNodes());
    
    }
    
    viewNodes() {
    this.myRes = this.structureRequest.result;
    this.contentArray = this.myRes.content;
    this.showAssigned = true;
     }
     }
    

2.这里是http服务,http get工作正常,收到所有数据:

import {Injectable} from '@angular/core';
import {Http, Response, Headers, RequestOptions} from '@angular/http';

import {Observable} from 'rxjs/Observable';

@Injectable ()
export class StructureRequestService {
result: Object;
//private myUrl = 'http://manny.herokuapp.com/audit/get/structure';
private myUrl = './app/home/nodes.json';  // local URL to structure APi
constructor (private http: Http) {
    //use XHR object
    let _build = (<any> http)._backend._browserXHR.build;
    (<any> http)._backend._browserXHR.build = () => {
        let _xhr =  _build();
        _xhr.withCredentials = true;
        return _xhr;
    };
}

sendRequest() {
    let body = JSON.stringify({});
    let headers = new Headers({ 'Content-Type': 'application/json'});
    let options = new RequestOptions({
        headers: headers
    });
     this.http.get(this.myUrl, options)
        .map((res: Response) => res.json())
        .subscribe(res => {this.result = res;
            return this.result; });
}
}

3.所以问题是同步步骤:接收数据,而不是查看它。

【问题讨论】:

标签: angular subscribe


【解决方案1】:

您可能希望在请求返回值时执行this.viewNodes而不执行this.viewNodes()的结果

这个

this.structureRequest.sendRequest().subscribe( this.viewNodes());

应该改为

this.structureRequest.sendRequest().subscribe(() => this.viewNodes());

前者执行this.viewNodes()并将结果传递给subscribe(),后者创建一个新的内联函数传递给subscribe()。这个内联函数在调用时会执行this.viewNodes()

如果你想传递sendRequest() 的值返回它应该是

this.structureRequest.sendRequest().subscribe((result) => this.viewNodes(result));

更新

sendReqeust() 不返回任何内容。

应该是

return this.http.get(this.myUrl, options) ...

但这只有在返回Observable 时才适用于您在代码中使用它的方式。

但最后的 subscribe() 返回一个 Subscription

 return this.http.get(this.myUrl, options)
    .map((res: Response) => res.json())
    .subscribe(res => {this.result = res;
        return this.result; });

因此应该改为

 return this.http.get(this.myUrl, options)
    .map((res: Response) => res.json())
    .map(res => {
      this.result = res;
      return this.result; 
    });

 return this.http.get(this.myUrl, options)
    .map((res: Response) => res.json())
    .do(res => {
      this.result = res;
    });

不同的是do()不修改流的值,也不需要返回任何东西。从.map() 返回的值将被转发。如果您想使用do,请确保已导入(如map)。

【讨论】:

  • 我试过 this.structureRequest.sendRequest().subscribe((result) => this.viewNodes(result));但是 this.viewNodes 没有启动
  • super.works!谢谢你很好的解释。我已经标记了你的答案。
  • 和往常一样,gunter 在这里对堆栈溢出的回复正在帮助很多人做一些有角度的事情
猜你喜欢
  • 2018-04-24
  • 2018-06-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-28
  • 2016-08-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多