2016-08-23 6 views
0

私はサービスの配列を見て、本当に苦労しています。角度のある時計の配列

サービス

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を更新したいです。

最初の読み込み時にwatch関数が呼び出されますが、データ読み込み時には呼び出されません。

$ watchを取得する簡単な方法はありますか?

答えて

1

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を参照してください。

関連する問題