2016-07-11 2 views
2

FireBaseを使用していてクエリを実行しようとすると、結果はログインしていますが、HTML $scopeには表示されません。

var shopRef = firebaseDataService.intro; 

$scope.shops = []; 

var taskRef = shopRef.orderByChild("cat").equalTo("Accomodation"); 


taskRef.on("value", function(snapshot) { 
    var snapData = snapshot.val(); 
    console.log(snapData); 
    $scope.shops.push(snapData); 

}); 

私は$scope.$apply()を使用し、私はお店に更新されたデータを取得するために管理し、それはまだ私のディレクティブには何も渡していません。

<search-card shops="shops"> </search-card> 
    <p> Shops are {{shops}}</p> 

私はそれが$ firebaseArray

$scope.shops = $firebaseArray(taskRef); 

で何とか働いて得たが、I`dはまだ私が間違ってやっていると、なぜそれがスナップショットで作業していないかを知るのが好き。 angularfire docsから

+0

として私を打つようあなたが$firebaseArray()オプションを使用して行くべきだと思うことが規定として$timeoutを使用してみてください代わりに


。$(適用)私は店舗に更新されたデータを取得することができますが、それでも私の指示には範囲が更新されていません。ディレクティブで$ scope。$ watchを指定しても。 – pecata

+0

どのような指令ですか? – adolfosrs

答えて

5

// read data from the database into a local scope variable 
ref.on("value", function(snapshot) { 

    // Since this event will occur outside Angular's $apply scope, we need to notify Angular 
    // each time there is an update. This can be done using $scope.$apply or $timeout. We 
    // prefer to use $timeout as it a) does not throw errors and b) ensures all levels of the 
    // scope hierarchy are refreshed (necessary for some directives to see the changes) 
    $timeout(function() { 
     $scope.data = snapshot.val(); 
    }); 
}); 

$scope.apply()を使用して階層全体(ひいてはディレクティブを)更新されませんようです。言われて、私は$スコープを使用する場合、それはほとんどの「角度」ソリューション

+0

と$ timeoutは動作していますが、Objectを返します。また、$ firebaseArrayがより良い解決策であるとも考えています。 – pecata