AngularFire - $ firebaseArray可以添加$,但不读 [英] AngularFire - $firebaseArray can $add but not read
问题描述
我觉得我必须失去了一些非常基本的。这是我服务的一部分:
I feel like I must be missing something very basic. Here is a portion of my service:
angular.module('fire')
.constant('FIREBASE_URI', 'https://___.firebaseio.com/')
.factory('syncArraySvc', function(FIREBASE_URI, $firebaseArray) {
var buildingsUri = FIREBASE_URI + "buildings";
var buildings = $firebaseArray(new Firebase(buildingsUri));
console.log(buildings);
var getBuildings = function() {
return buildings;
};
var addBuilding = function(item) {
buildings.$add(item);
};
return {
getBuildings: getBuildings,
addBuilding: addBuilding
};
});
在那中间的刚的console.log返回一个空数组。如果我尝试调用 syncArraySvc.getBuildings()
从另一个控制器的功能,我也得到一个空数组。不知怎的, $添加(项目)
的作品一样, syncArraySvc.addBuilding(项目)
。我缺少什么?
The console.log in the middle of that just returns an empty array. If I try to call the syncArraySvc.getBuildings()
function from another controller, I also get an empty array. Somehow, $add(item)
works, as does syncArraySvc.addBuilding(item)
. What am I missing?
推荐答案
其他答案帮助让我在正确的方向。
The other answers helped get me pointed in the right direction.
有$的 API文档C $ C样品似乎并不需要的数据被包裹在一个承诺:
The API documentation has a code sample that doesn't seem to need the data to be wrapped in a promise:
var list = $firebaseArray(new Firebase(URL));
$scope.list = list;
但是,它并指出,你可以使用的 $加载
承诺在加载数据时得到通知。这是我得到了它在我的项目的工作:
However, it does point out that you can use the $loaded
promise to be notified when the data is loaded. This is how I got it to work in my project:
syncArraySvc.getBuildings().$loaded(function(data) {
$scope.buildings = data;
});
我想在一个新的项目复制此,它一贯致力于而不 $加载
包装,就像他们在第一个例子说明。这是有道理的我, $加载
包装将需要。我不明白它如何能在第一个例子是工作离不开它。
I tried replicating this in a fresh project, and it consistently worked without the $loaded
wrapper, like they show in the first example. It makes sense to me that the $loaded
wrapper would be required. I don't understand how it could be working in the first example without it.
这篇关于AngularFire - $ firebaseArray可以添加$,但不读的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!