1

I'm starting a project with AngularJs (v 1). And that was nice until I started to wonder about the quality of my project. Because now I try to avoid abusing the $scope, $scope.$apply, $scope.broadcast and other nice things like that.

So now, all of my _controller_s are using var vm = this; instead of $scope and my directives are using callbacks instead of $scope.$apply. The problem I have is that the changes are not updated. While the usage of $scope.$apply from a model attribute inside a directive was automatically updating the views it is not the case with a callback to a controller that update the model attribute. Then a workaround is to use _event_s but since $scope.$broadcast and his friends is not recommended, I have to create my own observers.

But at the end, I have the feeling to loosing the cool features of AngularJs. And started to wonder how are professionnals doing.

  • How do you ensure that changes made to a model are updated to a view ?
  • How you ensure that changes made to a model from a view are updated to another (master-details) ?

Thanks a lot for your advices.

2
  • This is a fairly broad question. For general advice, the Angular 1 Style Guide was a big help for us. Commented Jul 1, 2016 at 13:52
  • Much too broad. Provide a concrete minimal example of something that doesn't work as you expect. With code. Commented Jul 1, 2016 at 13:56

1 Answer 1

1

Calbacks do not trigger angular digest. But events do not trigger digest either. The thing is that you usually want to call digest after: -user click -user input -http response -... And for all these cases there are built-in functions that trigger digest: $http, ng-click, ng-change, etc.

Sign up to request clarification or add additional context in comments.

2 Comments

So @Petr Averyanov, explicit calls to $scope.$applyis not that bad ?
explicit calls to $scope.$apply is not badat all, it is just not for beginners - you need to be 100% sure in what you are doing

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.