代码之家  ›  专栏  ›  技术社区  ›  ksliman

Angular2/4 http。获取对“this”的嵌套访问

  •  0
  • ksliman  · 技术社区  · 9 年前

     let headers = new Headers({ 'Authorization': `${this.user.Token}` });
            let options = new RequestOptions({ headers: headers });
            this.http.get(environment.apiRoot + 'Customers/Customer/Search/' + term + '/' + value, options)
                .map((response) => {
                    var Data = response.json().Data;
                    return Data;
                }).subscribe(Data => {
                    this.data.customer.emailAddress = Data.EmailAddress;
                    this.data.customer.phoneNumber = Data.PhoneNumber;
                    this.data.customer.firstName = Data.FirstName;
                    this.data.customer.lastName = Data.LastName;
                    this.data.customer.streetAddress = Data.StreetAddress;
                    this.data.customer.city = Data.City;
                    this.data.customer.state = Data.State;
                    this.data.customer.zipCode = Data.ZipCode;
                    this.data.customer.idNumber = Data.IDNumber;
                    this.data.customer.ID = Data.ID;
                    this.data.customer.comment = Data.Comment;
    
                    // //Check if customer has exsiting orders
                    this.http.request(environment.apiRoot + 'Orders/Order/Customer/' + Data.ID, options)
                        .subscribe(odResponse => {
                            var Data = odResponse.json().Data;
                            this.data.orders = Data;
                            this.existingOrders = Data.length > 0;
                        }, error => {
                            this.alertService.error('Encountered an error while trying to get customer order data');
                        });
                }, error => {
                    this.alertService.error('Encountered an error while trying to get customer data');
                });
    

    所以我想问题是如何在订阅中得到“this”作为组件而不是订阅。

    1 回复  |  直到 9 年前
        1
  •  0
  •   JeanPaul A.    9 年前

    let headers = new Headers({ 'Authorization': `${this.user.Token}` });
    let options = new RequestOptions({ headers: headers });
    
    this.http.get(environment.apiRoot + 'Customers/Customer/Search/' + term + '/' + value, options)
            .map((response) => { // This extracts the data you require
                var Data = response.json().Data;
    
                return Data;
            }).switchMap(Data => { // This returns the result of the second observable in 
                return this.http.request(environment.apiRoot + 'Orders/Order/Customer/' + Data.ID, options)
                                .map(odResponse => { // We have the response. Extract the json, and return an array with the first element as the old data, and the second element as the new data.
                                     var _Data = odResponse.json().Data;
                                     return [ Data, _Data ];
                                 });
            }).subscribe( data => {
                let firstSet = data[0];
                let secondSet = data[1];
                // Now do your assignment 
                this.data.customer.emailAddress = firstSet.EmailAddress;
                this.data.customer.phoneNumber = firstSet.PhoneNumber;
                ...
                ...
            });
    

    我使用这种方法是因为似乎需要第一次调用返回的数据(ID)才能进行第二次调用。如果不是这样,我会建议使用Observable.forkJoin。 This 是RxJS V4中forkJoin的文档,但它仍然适用于V5。

    推荐文章