服务中的角度观看阵列
问题描述:
我想观看服务中的阵列,真的很挣扎。服务中的角度观看阵列
的服务
angular.module('starter.entities',[]).service('Entities', function() {
var _entities = this;
// public API
this.locations = [];
document.body.addEventListener("dbready", function(){
buildModel(function(locations) {
_entities.locations = locations;
});
});
的控制器
.controller('DashCtrl', function($scope, Entities) {
$scope.$watch(function() { return Entities.locations }, function() {
doSomething...
}, true);
.....
Bascially它加载数据库中的数据,我想更新UI时加载数据。
监视功能在第一次加载发生时调用,但不会在数据加载发生时调用。
有没有简单的方法来获得$手表发生?
答
来自AngularJS框架之外的事件需要通知$apply
模型的变更框架。
app.service('Entities', function($document, $rootScope) {
var self = this;
// public API
this.locations = [];
$document.find("body").on("dbready", function(){
buildModel(function(locations) {
self.locations = locations;
//Use $apply to notify AngularJS framework
$rootScope.$apply();
});
});
欲了解更多信息,请参阅AngularJS $rootScope.scope API Reference -- $apply。