Angular 使用从服务器请求的值来创建类的实例
我想创建包含自定义类的类 这里我的类是客户,它包含的自定义类是国家 对于国家,我有以下类别:Angular 使用从服务器请求的值来创建类的实例,angular,observable,Angular,Observable,我想创建包含自定义类的类 这里我的类是客户,它包含的自定义类是国家 对于国家,我有以下类别: export class Country { constructor(public shortcut: string, public description: string) { } } 我从国家/地区服务获得国家/地区列表 import { Injectable } from '@angular/core'; import { Http } from '@angular/http'; im
export class Country {
constructor(public shortcut: string, public description: string) { }
}
我从国家/地区服务获得国家/地区列表
import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/of';
import 'rxjs/add/operator/map';
import { Country } from './country';
@Injectable()
export class CountryService {
countries: Country[];
constructor(private http: Http) {
this.getCountries().subscribe();
}
getCountries(): Observable<any> {
if(this.countries != null) {
return Observable.of(this.countries)
} else {
return this.http.get(`http://my-json-server.typicode.com/kolomu/CustomerDemoApp/countries`, { method: 'Get'})
.map( (response) => response.json() )
.map( (countries) => this.countries = countries)
}
}
getDescription(shortcut: string): Observable<string> {
if(this.countries != null) {
let tempCountry = this.countries.find( (e) => e.shortcut == shortcut);
return Observable.of(tempCountry.description);
} else {
this.getCountries().subscribe(
(countries) => {
let tempCountry = this.countries.find( (e) => e.shortcut == shortcut);
return Observable.of(tempCountry.description);
}
);
}
}
}
我的问题是,不知何故,措辞和方法调用不再有意义
请参阅下面的Customer类,了解我如何在getDescription调用中创建国家/地区
import { AppInjector } from './app-injector';
import { Country } from './country';
import { CountryService } from './country.service';
export class Customer {
public country: Country;
public countryService: CountryService;
constructor(public name: string, public countryShortcut: string){
this.countryService = AppInjector.get(CountryService);
this.countryService.getDescription(countryShortcut).subscribe(
countryDescription => {
return new Country(countryShortcut, countryDescription)
}
)
}
}
我的思维错误在哪里?也许用我自己的国家/地区实例创建客户的整个过程是错误的?非常感谢您的帮助
下面是该项目的github回购协议。
一些事情
import { AppInjector } from './app-injector';
import { Country } from './country';
import { CountryService } from './country.service';
export class Customer {
public country: Country;
public countryService: CountryService;
constructor(public name: string, public countryShortcut: string){
this.countryService = AppInjector.get(CountryService);
this.countryService.getDescription(countryShortcut).subscribe(
countryDescription => {
return new Country(countryShortcut, countryDescription)
}
)
}
}
getCountries(): Observable<any[]> { //<--return an array
if(this.countries != null) {
return Observable.of(this.countries);
} else {
return this.http.get(`http://my-json-server.typicode.com/kolomu/CustomerDemoApp/countries`) //we don't need json because we use httpClient
//If use "get" not put "method get"
.do((result)=>this.countries=result); //use "do" to do something more with the response. do NOT change the response
}
getDescription(shortcut: string): Observable<string> {
if(this.countries != null) {
let tempCountry = this.countries.find( (e) => e.shortcut == shortcut);
return Observable.of(tempCountry.description);
} else {
this.getCountries().map( //If not work try switchMap instead map
(countries) => {
let tempCountry = this.countries.find( (e) => e.shortcut == shortcut);
return tempCountry.description; //not Observable.of
}
);
}
this.country=new Country(...)