本文介绍了使用angularfire v2简单的firebase v3数据检索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
刚刚开始使用firebase v3(大约两周前发布)与angularfire v2(根据这个完全支持firebase v3)。但是我很努力地将一些数据检索到一个角度视图。这里是控制器,简单地返回用户数组并将其绑定到视图。它使用 $ firebaseArray
断行,并说: TypeError:预期的对象
我正在使用AngularJs 1.5.6。只是为了显示我使用的firebase和angularfire的版本:
< script src =https://cdn.firebase的.com /库/ angularfire / 2.0.1 / angularfire.min.js>< /脚本>
< script src =https://www.gstatic.com/firebasejs/live/3.0/firebase.js>< / script>
$ b $ var $ main函数($ scope,$ firebaseArray){
var root = firebase.database()。ref();
var users = root.child('users');
console.log(users); //这个工作正常,与firebase连接的问题
//注意:这不起作用并抛出异常
$ scope.users = $ firebaseArray(users);
}
app.controller(mainCtrl,[$ scope,mainCtrl]);
})();
解决方案
试试这个
app.controller(mainCtrl,[$ scope,'$ firebaseArray',mainCtrl]);
这应该是工作的
Just started using firebase v3 (released roughly two weeks ago) with angularfire v2 (which according to this post fully supports firebase v3). But I am struggling to simply retrieve some data into an angular view.
Here is the controller that simply returns user array and bind it to the view. It breaks on the line using $firebaseArray
and says: TypeError: Object expected
I am using AngularJs 1.5.6. Just to show the versions of firebase and angularfire I am using:
<script src="https://cdn.firebase.com/libs/angularfire/2.0.1/angularfire.min.js"></script>
<script src="https://www.gstatic.com/firebasejs/live/3.0/firebase.js"></script>
(function() {
var app = angular.module("FirebaseTest");
var mainCtrl = function($scope, $firebaseArray) {
var root = firebase.database().ref();
var users = root.child('users');
console.log(users); // this works fine so no problem with firebase connection
//NOTE: this doesn't work and throws exception
$scope.users = $firebaseArray(users);
}
app.controller("mainCtrl", ["$scope", mainCtrl]);
})();
解决方案
try this
app.controller("mainCtrl", ["$scope", '$firebaseArray', mainCtrl]);
This should work
这篇关于使用angularfire v2简单的firebase v3数据检索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!