将$ scope变量设置为service / factory变量的更新值的问题

时间:2014-10-21 04:57:27

标签: javascript angularjs angularjs-scope firebase angularjs-service

非常感谢一些帮助:

我目前正在使用Firebase作为通过我的工厂检索的数据的后端:

var schedulesFactory = function($firebase, FIREBASE_URL) {

    var ref = new Firebase(FIREBASE_URL);

    var schedules = $firebase(ref.child('schedules')).$asArray();

    var week = [];

    var getWeek = function() {
        return schedules.$loaded(function(data) {
            var date = new Date();
            for (var i = 0, len = data[2].length; i < len; i++) {
                for (var j = 0, len2 = data[2][i].games.length; j < len2; j++) {
                    if (Date.parse(date) < Date.parse(data[2][i].games[j].scheduled)) {
                        week = data[2][i].number;
                        return;
                    }
                }
            }
        });
    };

    return {
        week: week,
        getWeek: getWeek,
        schedules: schedules
    };
};

angular
    .module('nflApp')
    .factory('schedulesFactory', ['$firebase', 'FIREBASE_URL', schedulesFactory]);

现在这是我的控制器:

var SchedulesCtrl = function($scope, schedulesFactory) {

    $scope.schedules = schedulesFactory.schedules;

    schedulesFactory.getWeek().then(function() {
        $scope.week = schedulesFactory.week;
    });

};

angular
    .module('nflApp')
    .controller('SchedulesCtrl', ['$scope', 'schedulesFactory', SchedulesCtrl]);

一旦执行,我会期望$ scope.week的值在检索到数据并且getWeek()得到评估后等于schedulesFactory.week的值。但是,$ scope.week仍然是一个空数组,而schedulesFactory.week正确设置其值来自getWeek()。我在这做错了什么?

感谢您的任何意见!

0 个答案:

没有答案