火力地堡的在AngularJS服务AngularFire [英] Firebase's AngularFire in an AngularJS service
问题描述
在AngularJS处理火力地堡肯定是最好的办法必须是从服务中,所以它可以在整个应用程序。
The best way of handling Firebase in AngularJS surely has to be from within a service, so it's available to all Controllers across the App.
我只是无法得到它的工作! ......我第一次使用 angularFire(新火力地堡(URL))
试过了,希望我能绑定到该服务的范围,但角抱怨说,它不能 $看
了。
I just can't get it to work! ... I first tried using angularFire(new Firebase(url))
, hoping I could bind to the service's scope, but Angular complains that it cannot $watch
it.
所以,我想angularFireCollection,而不是像这样:
So I tried angularFireCollection instead like this:
app.factory('myService', function myService(angularFireCollection) {
var url = 'https://myfirebase.firebaseio.com';
return {
getAll: function(path) {
var ref = angularFireCollection(new Firebase(url + '/' + path));
console.log(ref);
return ref;
},
...
};
});
不过,angularFireCollection是包含方法的负载等等,如果我把它绑定到一个控制器$范围我只是得到垃圾对象。这也抱怨说,它不能调用火力地堡功能之前,我尝试使用它们(例如错误:Firebase.push失败:第二个参数必须是一个有效的函数
)。 ..任何人都得到了,我要去哪里错了什么想法?
However, the angularFireCollection is an Object containing a load of methods etc. if I bind it to a controller $scope I just get garbage. It also complains that it can't call the Firebase functions before I try to use them (e.g. Error: Firebase.push failed: second argument must be a valid function.
)... anyone got any ideas where I'm going wrong?
查看此 PLUNKER
See this PLUNKER
推荐答案
如果你想封装了一些功能集成到一个服务,可以考虑保持返回裁判在服务的状态。我扩展了您的plunker。这似乎大多是做什么你尝试了。
If you want to encapsulate some of the functionality into a service, consider keeping the returned ref in state of the service. I expanded on your plunker. It seems to mostly do what you were trying for.
这篇关于火力地堡的在AngularJS服务AngularFire的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!