Search code examples
angularangular2-templateangular2-servicesangular2-directives

I want to store component.ts data in to another variable and i want to convert into Global Variable


get_update(id:any): Observable<any[]>{


      let headers = new Headers({ 'Content-Type': 'application/json'});
      let options = new RequestOptions({ headers: headers });
      return this.http.get('http://localhost:8000/vendor/'+id,options)
                        .map(response => response.json())                        
                        .catch(error => Observable.throw(error.statusText));     


}

component.ts

ngOnInit()
{ this.service.get_update(this.user).subscribe(data => console.log(data));
}

component.ts datas to be stored in another variable


Solution

  • Declare a data member in your class and use it, and for sharing data you can use @input() decorator @outputdecorator or use a service.

        export class AppComponent {
          constructor(){}
          public data=[];//
    
    
        ngOnInit()
        { this.service.get_update(this.user).subscribe(data => this.data=data);
    }