h. Oo h. Oo - 6 months ago 26
AngularJS Question

$apply unknown provider error

i'm trying to build a very simple chat with

express.js
,
socket.io
and
angular
. it works. the only problem that i have is when the socket message event is fired it isn't synchronizing and rendering it onto the page.

var socket = io.connect('http://localhost:8080');



angular.module('chat',[]).controller('chatController',['$scope','$apply',function($scope,$apply){
chat = $scope;
chat.messages = [];

socket.on('messages',function(data){
chat.$apply(function(){
chat.messages.push(data);
});
});
}])


i know i should use $apply somehow but it gives me an error of unknow provider.
what is the correct way for implementing
$apply

Answer

No need to pass $apply as a dependency,

Try this

angular.module('chat',[]).controller('chatController',['$scope',function($scope){
     $scope.messages = [];
     socket.on('messages',function(data){
         $scope.$apply(function(){
             $scope.messages.push(data);
        });
    });
}])