使用angularFire时“undefined不是函数”错误

时间:2013-09-18 04:44:09

标签: angularjs angularfire

我正在尝试显示来自firebase的数据,我有以下代码。我已经为我的应用程序声明了firebase依赖项。

.controller('AdvMainCtrl', ['$scope', 'dataLoad', 

    function($scope, dataLoad, $route, $routeParams, $location, $resource, angularFire) {
        var categoryPromise = dataLoad.loadCategories($scope, 'categories'),
        tmPromise = dataLoad.loadTransportationMode($scope, 'tModes'),
        tourPromise = dataLoad.loadAdventures($scope, 'tours');
        categoryPromise.then(function() {
            console.log('data loaded');
        });

        $scope.$watch('category', function() {
            if (typeof $scope.category === 'undefined') return;
            console.log('category changed');
            console.log($scope.category.name);
        });
        $scope.$watch('tMode', function() {
            if (typeof $scope.tMode === 'undefined') return;
            console.log('tm changed');
            //console.log($scope.transportationMode.name);
        });
    var ref = new Firebase("https://wpladv.firebaseio.com/adventure");
    $scope.tours = [];
    angularFire(ref, $scope, "tours");
    }
])

在控制台中我看到angularFire(ref, $scope, "tours");语句发生错误。我不知道如何解决这个问题。

我的控制器中的整个代码是

.controller('AdvMainCtrl', ['$scope', 'dataLoad',
        function($scope, dataLoad, $route, $routeParams, $location, $resource, angularFire) {
            var categoryPromise = dataLoad.loadCategories($scope, 'categories'),
            tmPromise = dataLoad.loadTransportationMode($scope, 'tModes'),
            tourPromise = dataLoad.loadAdventures($scope, 'tours');
            categoryPromise.then(function() {
                console.log('data loaded');
            });

            $scope.$watch('category', function() {
                if (typeof $scope.category === 'undefined') return;
                console.log('category changed');
                console.log($scope.category.name);
            });
            $scope.$watch('tMode', function() {
                if (typeof $scope.tMode === 'undefined') return;
                console.log('tm changed');
                //console.log($scope.transportationMode.name);
            });
        var ref = new Firebase("https://wpladv.firebaseio.com/adventure");
        $scope.tours = [];
        angularFire(ref, $scope, "tours");
        }
    ])

错误显示在“var categoryPromise = dataLoad.loadCategories($ scope,'categories'),”statement

我的api js文件中有以下代码。

angular.module('localAdventuresApp')
    .factory('dataLoad', ['angularFire',
        function(angularFire) {
            var dbUrl = 'https://wpladv.firebaseio.com/';

            return {
                loadCategories: function(scope, items) {
                    var cat = '/category';
                    return angularFire(dbUrl + cat, scope, items, []);
                },
                loadTransportationMode: function(scope, items) {
                    var cat = '/transportMode';
                    return angularFire(dbUrl + cat, scope, items, []);
                },
                loadAdventures: function(scope, items) {
                    var cat = '/adventures';
                    return angularFire(dbUrl + cat, scope, items, {});
                }
            }
        }
    ])

错误显示在“return angularFire(dbUrl + cat,scope,items,[]);”声明在这里。我在控制台中看到的错误是“错误:请提供Firebase引用而不是URL,例如:new Firebase(url)”。

2 个答案:

答案 0 :(得分:3)

您需要将依赖项注入控制器

.controller('AdvMainCtrl', ['$scope', 'dataLoad', '$route', '$routeParams', '$location', '$resource', 'angularFire',
    function($scope, dataLoad, $route, $routeParams, $location, $resource, angularFire) {
        // your code here
    }
])

答案 1 :(得分:2)

错误“请提供Firebase参考而不是网址,例如:新的Firebase(网址)”正在发生,因为我有角火版本> 0.3.0。我所要做的就是将dbUrl + cat更改为新的Firebase(dbUrl + cat)。这解决了这个问题。谢谢大家的宝贵建议。

更改后的代码

angular.module('localAdventuresApp')
    .factory('dataLoad', ['angularFire',
        function(angularFire) {
            var dbUrl = 'https://wpladv.firebaseio.com';

            return {
                loadCategories: function(scope, items) {
                    var cat = '/category';
                    console.log(dbUrl);
                    var ref = new Firebase(dbUrl + cat);
                    return angularFire(ref, scope, items, []);
                },
                loadTransportationMode: function(scope, items) {
                    var cat = '/transportMode';
                    var ref = new Firebase(dbUrl + cat);
                    return angularFire(ref, scope, items, []);
                },
                loadAdventures: function(scope, items) {
                    var cat = '/adventure';
                    var ref = new Firebase(dbUrl + cat);
                    return angularFire(ref, scope, items, {});
                }
            }
        }
    ])