AngularJS 2 - 调用RESTful服务 - Fat Arrow

时间:2016-10-26 00:05:40

标签: angularjs rest

希望有人可以帮助我。我对AngularJS2完全陌生,如果我不提供所需的一切,请原谅我。

很简单,我正在调用RESTful服务来获取一些数据。 RESTful调用很好,正确的数据返回到组件。然后,当我尝试获取该信息并将其存储在类级别变量中时,该变量始终保持为" undefined"。我似乎可以从组件调用中获取RESTful数据。

有人可以帮忙吗?我究竟做错了什么?我确定我在某个地方弄乱了胖箭。请记住,这是AngularJS2而非ver 1。

谢谢。

组件:

import { Component, OnInit } from '@angular/core';
import { Http, Response, HttpModule } from '@angular/http';

import { LocationDetails } from './models/locationdetails';
import { LocationService } from './services/location.service';

@Component({
    selector: 'my-app',
    template: '<h1>My First Angular App</h1>',
    providers: [LocationService],
})
export class AppComponent implements OnInit {

    public locationDetails: LocationDetails[];

    constructor(private _locationService: LocationService) {
        this._locationService = _locationService;
    }

    ngOnInit() {
        this.getLocations();

    }

    private getLocations = (): void => {
        this._locationService.getLocationsService()
            .subscribe((response: LocationDetails[]) => { this.locationDetails = response; },
            error => console.log(error),
            () => console.log(this.locationDetails));  //<-- this.locationDetails value has the return data from the RESTful

        **console.log('HELLO ' + this.locationDetails); //<-- this.locationDetails is now undefined. Was fine just one line up.**
    }

}

服务

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

import { Observable } from 'rxjs/Rx';

import { Configuration } from '../shared/app.configuration';
import { LocationDetails } from '../models/locationdetails';

@Injectable()
export class LocationService {
    private _url: string

    constructor(private _http: Http, private _configuration: Configuration) {
        this._http = _http;
        this._url = _configuration.baseUrl;
    }   

    public getLocationsService = (): Observable<LocationDetails[]> => {
        return this._http.get(this._url)
            .map((response: Response) => <LocationDetails[]>response.json())
            .catch(this.handleError);
    }

    private handleError(error: Response) {
        debugger;
        console.error(error);
        return Observable.throw(error.json().error || 'Server error');
    }
}

MODEL

export class LocationDetails {
    Active: boolean;
    AutoTransfer: boolean;
    CompanyNumber: number;
    CurrencyID: number;
    EntityID: number;
    IndividualTaxesEnabled: boolean;
    IsPrimaryState: boolean;
    LocationCode: string;
    LocationID: number;
    LocationName: string;
    MTP_Address: string;
    MeasurementOptionID: number;
    PriceRateID: number;
    RegionID: number;
    RemitAddressTypeID: number;
    RemitToAddressEnabled: boolean;
    SMTP_Address: string;
    SMTP_Port: string;
    StateProvince: string;
    bCustomPurchaseOrderRemitEnabled: boolean;
    txtInvoiceFooter: string;
    vcDefaultTaxLabel: string;

}

0 个答案:

没有答案
相关问题