LetsLunch / lunch

Other
38 stars 3 forks source link

understand $scope.$apply more thoroughly #76

Open cameron opened 10 years ago

cameron commented 10 years ago

https://github.com/LetsLunch/lunch/blob/development/app/scripts/noshow.js#L18

this code shouldn't work — you're modifying the scope object outside of angular's digest cycle, which means angular isn't aware that anything on the scope object has changed, and it won't rerender the page (unless something else is triggering a digest, which is quite possible, and would make this mistake invisible).

this is what angular's $interval service is for, but you should know that anytime you write asynchronous code outside of angular's builtins like $http and $interval that modifies a $scope object, you'll need to invoke $scope.$apply() to let angular know that you made changes that it should reflect. here's a decent article on the topic.

zzmp commented 10 years ago

Omkar, show us your angular-fu!