When I call the ngOnInit() function, data is fetched from a service and stored for use in my component. However, I am encountering issues with accessing this data outside of the subscribe method. I am unsure of what the problem might be.
import { Component, OnInit, Renderer2, HostListener,ViewChild, TemplateRef, Inject} from '@angular/core';
import { DOCUMENT } from '@angular/common';
import { NgbModalOptions, NgbModal } from '@ng-bootstrap/ng-bootstrap';
@Component({
selector: 'app-pattern',
templateUrl: './pattern.component.html',
styleUrls: ['./pattern.component.scss']
})
export class PatternComponent implements OnInit {
public config: any = {};
constructor(@Inject(DOCUMENT) private document: any,private modal: NgbModal,private pattern:patternService) { }
ngOnInit(): void {
this.pattern.Pattern(this.local).subscribe(
data => {
for (var i = 0; i < this.length1; i++){
this.visitedArray[temp1] = [false, this.Patternarray[i]]; //I'm trying to access this in onselect() function. If the value is false I'm trying to do something
console.log("Visited Array ng:",this.visitedArray[temp1]);
}
},
err => {
console.error(err);
}
);
}
}
Within my onselect() function, I attempt to retrieve the data stored in this.visitedArray[this.temp1] from the ngOnInit method. Unfortunately, I am unable to access it successfully and the console displays 'Visited Array:' as undefined.
onselect(row, i) {
console.log("Visited Array:",this.visitedArray[this.temp1]);
console.log("visited id:",this.visitedArray[this.temp1][0]);
}