Are there any suggestions on how to make a dynamic array available globally in Angular? I am currently using this codepen () which stores clicked countries in an array.
The issue is that the array is nested within a function in ngOnInit and I need it to be accessible globally to share between components. I have tried moving the empty array outside the function and making it equal to a global variable without success. Is there a way to update a global array from this nested position?
Map Component
import { Component, OnInit } from '@angular/core';
import { Router, RouterModule, ActivatedRoute } from '@angular/router';
import { AmChartsService } from "amcharts3-angular2";
declare var AmCharts : any;
@Component({
selector: 'app-map',
templateUrl: './map.component.html',
styleUrls: ['./map.component.css']
})
export class MapComponent implements OnInit {
selectedCountries: any;
constructor()
{}
ngOnInit() {
// code for map settings
function getSelectedCountries() {
var selected = [];
for(var i = 0; i < map.dataProvider.areas.length; i++) {
if(map.dataProvider.areas[i].showAsSelected)
selected.push(map.dataProvider.areas[i].enTitle);
}
return selected;
}
}
}