In Angular, we can use $apply or $digest to update the DOM. However, I want to practice coding without using $scope so as to be easier to adapt to Angular 2 but I don't know how to update the DOM followed by a HTTP request!!
Asked
Active
Viewed 237 times
0
-
Take a look at http://stackoverflow.com/questions/26921058/alternative-to-scope-in-angularjs-2-0 for more information. Actually in angular1, using $apply or $digest is a bad practice. Dunno about angular2 – Okazari May 26 '15 at 16:02
-
most things will trigger a $digest on their own.. The case where I could see this needing to be done would be you are storing values in a service which is being referenced in your controller. Maybe you could post the specific example. – tpie May 26 '15 at 16:04
-
Also in this blog http://angularjs.blogspot.fr/ the team said "t's too soon to start building anything with 2.0 code -- we're still in the very early stages of building out the project." So i woudn't worry that much about no using scope. $scope will exist but in a different way. Anyway it's up to you. – Okazari May 26 '15 at 16:06
1 Answers
0
If you do not want to use $scope
to bridge between your controller and view, you may use controller as
syntax:
<body ng-controller="MainCtrl as main">
<p>Hello {{main.name}}!</p>
</body>
In your controller, it's good practice to bind this
to self
or some other var you will recognize so you can reference them later on. You can use this
but it can get confusing and complicated to work with if you don't bind it to a var.
app.controller('MainCtrl', function($scope) {
var self = this;
self.name = 'test'
});
I'm not sure that this pattern will get you used to angular 2, but it is considered by some as a better way to code in angular.
If for some reason you NEED to fire a $digest, I think the generally accepted hacky way is to run a $timeout when you need to fire the $digest. $timeout inherently calls one.
$timeout(function(){},0);
I would probably just set up a $watch though, if I needed to keep an eye on some changing data.

tpie
- 6,021
- 3
- 22
- 41
-
this work perfectly but if the binding is in a callback of a request, it doesn't a change – Chu Son May 26 '15 at 16:11