1
I’m having a problem using an Observable. While inside the Ngoinit method, when fetching the data, I get the array correctly and Seto in the class variable. However, when trying to access data outside of the subscribe, I get Undefined as a response. Can anyone tell why this occurs?
@Component({
moduleId: module.id,
selector: 'operacao',
templateUrl: './operacao.component.html',
providers: [Operacao],
styleUrls: ['./operacao.component.css']
})
export class OperacaoComponent implements OnInit {
private _operacao: Operacao;
private _operacoes: any[];
public rows: Array<any> = [];
private myserviceService: MyserviceService;
...
constructor(myservice: MyserviceService) {
this.length = 10;
this.myserviceService = myservice;
}
...
ngOnInit() {
this.myserviceService.getOperacoes()
.subscribe((operacao) => {
this._operacoes = operacao;
//here i get the "right" value
console.log(this._operacoes);
//Array(2) [Object, Object]
//0:Object {id: 0, acao_id: 0, user_id: "1", …}
//1:Object {id: 1, acao_id: 1, user_id: "1", …}
myservice.ts
getOperacoes() {
return this.http.get(this.url + 'operacoes').map(res => res.json());
}
when I try to access the data outside the observable, I receive Undefined.
console.log(this._operacoes);
Felipe, thank you for your reply.
– tecnocrata
@technocrat, if the answer solved your problem, can accept it, please?
– Felipe Avelar
Sorry, I thought you said yes. However, debugging the problem...I checked that it was not in the observable (when creating a test html, with databinding the data was displayed normally) but in the module (ng-table) that I use. I think I will have to change the onChangeTable method used by the module. Anyway, your answer led me to the right path! Grateful!
– tecnocrata