Let’s say you have a list of numbers displayed with ng-repeat and you want to shift one off the list when the user presses the right arrow key, and redisplay them one at a time if the user presses the left arrow key. You could bring the livereload feature with gulp or grunt or any other framework for building front-end solutions. So it requires a call to $scope.$apply to stay in sync. This is because Angular has what is known as a digest-watch cycle where all of this gets figured out:Īs automagical as Angular is in some ways, it has no way of knowing when your property has been updated outside of Angular-land (and sometimes doesn’t even bother when it is updated in Angular-land, as per the example that follows). The issue is around updating properties on $scope, either in directives or in controllers, and not having the updated changes be reflected on the front-end in the manner which you expect (either they will not show up at all, or they will happen in an order which you do not anticipate, which will cause bugs). So, having been bit by the issue multiple times, I recommend trying a call to $scope.$apply (either wrap the changes to $scope properties inside a $scope.apply callback, or call $scope.$apply on its own after $scope properties have been updated) See the documentation here. After all, it’s not really needed for the todo-list app of yore but it becomes much more important when you are doing funny things like manipulating scope deep inside of directives and so on. Use $scope.$applyĭuring your first foray into Angular you will probably not come across this as it is one of those hidden, quasi-leaky-abstraction sort of things that only becomes well known to you as you work on getting a non-trivial app off the ground. Since I don’t really like “magical” or knee-jerk fixes to problems I highly recommend Jim Hoskins’s article on $scope.$apply which you can find here. This solution is to make sure $scope.$apply is getting used in the proper manner when updates to $scope are happening, especially if they are happening in unusual places e.g. In particular I had one issue that kept cropping up so often I began trying it before running to Google for help if something wasn’t working the way I would have expected (all my views should just magically sync up with what’s on $scope, right?). When you start to form a basic mental model of how Angular works and you hit your first stumbling block where your model turns out to be incorrect it can be really, really, frustrating. livereload extensions 2.1.0 rails v4.2.5.1 guard v2.13.0 guard-livreload v2.5.1 chrome browser v48 2 It turned out that when I upgraded by hard drive from a mechanical to a SSD drive, this problem stopped occuring. As I’ve gotten a little into AngularJS I’ve been surprised by how often my assumptions about how things will work have turned out to be wrong.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |