I have a problem that I update this.customers in my controller but it does not update in the view
The relevant functions are:
//support.js
(function(){
'use strict';
var SupportCtrl = function($state, SocketFactory){
this.user = "Will"
this.customers = ['will','tom']
SocketFactory.on('queue', function(queue){
console.log(queue)
this.customers = queue
})
}
angular
.module('support',[])
.config(['$stateProvider', function($stateProvider){
$stateProvider
.state('support',{
url: '/support',
templateUrl: 'modules/support/support.html',
controllerAs: 'support',
controller: 'SupportCtrl'
})
}])
.controller('SupportCtrl', [
'$state',
'SocketFactory',
SupportCtrl
])
})()
//socketService.js
(function(){
'use strict';
var SocketFactory = function($rootScope){
var socket = io.connect();
return {
on: function (eventName, callback) {
socket.on(eventName, function () {
var args = arguments;
console.log($rootScope)
$rootScope.$apply(function () {
callback.apply(socket, args);
});
});
},
emit: function (eventName, data, callback) {
socket.emit(eventName, data, function () {
var args = arguments;
$rootScope.$apply(function () {
if (callback) {
callback.apply(socket, args);
}
});
})
}
};
};
angular
.module('ecomApp')
.service('SocketFactory', [
SocketFactory,
]);
})();
//support.html
<div>
<li ng-repeat="(roomNameKey, roomName) in support.customers">{{roomNameKey}}: {{roomName}}</li>
</div>
Diagnosis
this.customers prints to the screen 0: will 1: tom But on the Socket 'queue' event the object:
{roomName: "ikp3f"}
is successfully console.logged but the angular view is not updated
I suspect this might have to do with the digest cycle and $apply() - do I need to call $apply() in my SupportCtrl?
Thanks for the help
socketService.jscontent?.service('SocketFactory', SocketFactory). With this$injectorwill try to guess your parameters based on their names.