How can I store the value of msg
in the variable sample
when sample
is not accessible inside the callback function?
import { Injectable } from '@angular/core';
import * as socketCluster from 'socketcluster-client';
@Injectable({
providedIn: 'root'
})
export class DatamanagerService {
socket = socketCluster.create({
port: 8000
});
sample:string;
constructor() { }
connect() {
this.socket.on('connect', () => {
console.log('CONNECTED');
});
this.socket.on('random', (msg) => {
console.log('RANDOM: ' + msg);
this.sample = msg; // <- issue here
});
}
}