“TypeError.parent.context.car.getBrands is not a function”: s

后端 未结 1 1193
一个人的身影
一个人的身影 2021-01-26 09:13

Service:

@Injectable()
export class Service {

    constructor(private http: Http) { }

    getCars(){
        return this.http.get...       
    }

    getById         


        
1条回答
  •  醉梦人生
    2021-01-26 09:33

    I suppose the likely problem is located here:

     this._service.getById(id)
        .subscribe(car => {
          this.car = car; // you just init object like { id: '1', name: 'name'}
        });
    

    Probably your service looks like this:

    getById(id: string) {
       return this.http.get('app/car.json')
         .map(data => data.json());     
    } 
    

    Try to get Car model from service like this:

    getById(id: string) {
       return this.http.get('app/car.json')
         .map(data => data.json())
         .map(car => new Car(car.id, car.name));  <== add this line   
    }   
    

    This way car will be instance of Car class and will have appropriate methods like getBrands, addBrand...

    Hope it helps!

    0 讨论(0)
提交回复
热议问题