Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 使用从服务器请求的值来创建类的实例_Angular_Observable - Fatal编程技术网

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回购协议。 一些事情

  • 使用httpClient,而不是http。因此,您不需要使用response.json()
  • 如果您想订阅返回数组的可观察对象,请使用Observable 3.-映射更改响应。如果您想对响应执行某些操作,请使用do
  • L

  • 在我看来,您不需要类,只需要接口(类中没有方法)
  • 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(...)