Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

$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

like image 260
h. Oo Avatar asked May 03 '26 22:05

h. Oo


1 Answers

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);
        });
    });
}])
like image 78
Sajeetharan Avatar answered May 06 '26 11:05

Sajeetharan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!