resolveDependencies = function ($q, $rootScope, dependencies) {
var defer = $q.defer();
require(dependencies, function () {
defer.resolve();
$rootScope.$apply()//
});
return defer.promise;
var defer = $q.defer();
require(dependencies, function () {
defer.resolve();
$rootScope.$apply()//
});
return defer.promise;
};
我们都知道AngularJS 的一个很重要的特新是双向数据绑定,双向数据绑定意味着当view中有任何数据发生了变化,那么这个变化也会自动地反馈到scope数据上,反之一样,当scope模型数据发生变化,也会反映在view中更新。那这个是如何实现的呢?在调用$scope.$apply()后,启动$rootScope.$digest().所以。一轮$digest循环在$rootScope开始,随后会访问所有的children scope中的watchers.当一个watcher被触发时,AngularJS会检测scope模型,如果发生改变则调用相应的回掉函数。
那好了,为什么用$rootScope.$apply();而不是$scope.$apply().用$rootScope是因为当我多个controllers使用同一个service的时候不用担心service使用哪个scope