Currently delving into Angular 2, I find myself in need of assigning the data obtained from my service to a variable within the constructor of my component. How can I effectively utilize the subscribe function in order to assign these values to a public data array?
public data: Array<any> =[];
public constructor(private _languageService: LanguageService){
this._languageService.getLanguages()
.subscribe(languages => {this.languages = languages;}
,error => this.errorMessage = <any>error)
this.length = this.data.length;
//Is there a way for me to set this.data equal to this.languages
//if this.languages remains empty outside of the subscribe function?
}
I would greatly appreciate any assistance on how to effectively assign the returned data from a service to a variable.