Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update $scope from inside callback in angularJS

Tags:

I have this controller and I would like to update the $scope.progress from within the function callback. I tried using $rootScope and $scope.apply() but I can't get it to work. Is there something I'm missing?

progressupdate is a variable returned by the event. the code is not exactly like this. I made it super simple here to show the structure.

app.controller('player', function($scope) {      var show = function(url) {         function(err, showOK) {             if (err) {                 console.log(err);             } else {                 showOK.on('listening', function(){                      $scope.progress = progressupdate;                 });             }         });     }      show(url);  }); 

Am I running that function incorrectly inside the controller? Should I use something like this?

 $scope.show = function(url)...etc 
like image 588
fmtoffolo Avatar asked Mar 31 '14 19:03

fmtoffolo


1 Answers

I don't see the $apply function on above script, and progressupdate. Try $apply after set, or set it inside $apply :

showOk.on('listening', function(){    $scope.$apply(function(){       $scope.progress = progressupdate;    }); }); 

or

showOk.on('listening', function(){    $scope.progress = progressupdate;    $scope.$apply(); }); 

The first method is recommended.

like image 52
Iqbal Fauzi Avatar answered Oct 19 '22 00:10

Iqbal Fauzi