angularjs - When is $scope.$apply necessary for dealing with objects/arrays with Angular? -



angularjs - When is $scope.$apply necessary for dealing with objects/arrays with Angular? -

i'm working soundcloud js sdk bring soundcloud favorites simple angular app.

i wasn't able user favorites import in correctly until used $scope.$apply.

function toplistctrl($scope, $http, $modal) { $scope.getdata = function(sc_user) { sc.get('/users/'+ sc_user +'/favorites', {limit: 200}, function(tracks){ $scope.$apply(function() { if (object.getownpropertynames(tracks).length > 1) { $scope.likes = tracks; $scope.sortfield = 'like.favoritings_count'; $scope.reverse = true; $scope.sc_user = sc_user; } else { alert("that user has 0 soundcloud likes. sad...") } }).error(function (data, status, headers, config) { alert("something went awry request. double check that's real soundcloud username"); }) }); }

if don't utilize $scope.apply, doesn't work (and says sc.get not defined).

i'd understand bit improve why $scope.$apply necessary. inquire because when using http api, didn't need it.

function toplistctrl($scope, $http, $modal) { $scope.getdata = function(sc_user) { var url = 'http://api.soundcloud.com/users/'+ sc_user +'/favorites.json?client_id=0553ef1b721e4783feda4f4fe6611d04&limit=200&linked_partitioning=1&callback=json_callback'; $http.jsonp(url).success(function(data) { if (object.keys(data.collection).length > 0) { $scope.likes = data; $scope.sortfield = 'like.favoritings_count'; $scope.reverse = true; $scope.sc_user = sc_user; } else { alert("that user has 0 soundcloud likes. sad...") } }).error(function (data, status, headers, config) { alert("something went awry request. double check that's real soundcloud username"); }); }

usually angular knows code that's executing because you're 1 providing function callbacks it's angular that's calling them. after angular calls function, phone call $apply sometime later trigger $digest cycle.

if don't know $digest cycle is, concept simple. during $digest phase, angular dirty check on every scope variable that's been set $watch handler , check if it's changed; if has angular phone call corresponding $watch handler update view.

getting original question - when angular knows code, trigger $digest cycle - there no need phone call $apply explicitly. if handle jquery event, that's different story. angular has no thought $digest might needed - how can it? $apply needed trigger $digest manually.

angularjs angularjs-scope soundcloud

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -