Using TypeScript and Angular, I am trying to create an empty array within my testConnection function that allows the addition of objects every time the function is called, without clearing the array.
testConnection function:
testConnection(system) {
var systemList = [];
this.service.testConnection(system)
.subscribe(conn => {
if (conn == 'Success') {
this.snackBarHandler.open('Connection Found', 'success');
system.isClicked = false;
system.connection = true;
systemList.push(system);
}
else {
this.snackBarHandler.open('Connection Failed', 'failure');
system.isClicked = false;
system.connection = false;
systemList.push(system);
}
}, err => console.log(err));
}
Currently, with the logic I have, it is adding the system object to the array, but since the empty array declaration is within the function, it is clearing and restarting this process every time the function is called. I have tried to declare the systemList at the top of the class (systemList = any[]), but when I try to reference it within the function, it is showing up as undefined.
How am I able to add system objects to the array any time the function is called, without clearing the existing objects out of the array?