无法在角度控制器中识别工厂

时间:2013-12-20 21:38:11

标签: javascript angularjs angular-seed

我正在尝试使用工厂将数据从一个控制器传递到另一个控制器,由于某种原因,我的工厂没有被角度种子识别。这是我的app.js文件,我正在宣布我的工厂

var myApp = angular.module('myApp', ['myApp.controllers','angularFileUpload', 'ngRoute']);
//
//
myApp.config(['$routeProvider', function($routeProvider) {
    $routeProvider.when('/' ,{templateUrl:"/syncproto/partials/videoSlides.html"}, "sync").
        when('/scenarios', {templateUrl:"/syncproto/partials/scenarios.html"}, "scenariosCtrl").
        when('/scenarios/:id', {templateUrl:"/syncproto/partials/scenarios.html"}, "scenariosCtrl")
}]);

myApp.factory('Data', function() {
    var Data;
    return {
        getData: function () {
            return Data;
        },
        setData: function(value) {
            Data = value;
        }
    };
});

这是我正在使用工厂数据的控制器

.controller('scenariosCtrl', ['$scope', '$http', '$location', 'Data', function($scope, $http, Data) {
        $scope.scenarios = [];
        $scope.thumbnails = [];
        $scope.init = function(){
            console.log('init hit');
            $http({ method: 'GET', url: 'http://localhost:3000/scenarios' }).
                success(function (data, status, headers, config) {
                    angular.forEach(data, function(scenario) {
                        if (scenario.video.length != 0 && scenario.video[0].thumbnailLocation != undefined){
                        $scope.thumbnails.push(scenario.video[0].thumbnailLocation);
                        //console.log('thumbnail is' + scenario.video.thumbnailLocation);
                            $scope.scenarios.push(scenario);
                            console.log(scenario.video[0].thumbnailLocation);
                        }
                    });
                    //console.log($scope.scenarios);
                    console.log('success');
                }).
                error(function (data, status, headers, config) {
                    console.log('error');
                });
        console.log($scope.thumbnails);
        }

        $scope.showVideo = function(scenario){
            Data.setData(scenario);
            $location.path('/');

            //Data.setData($scope.scenarios[$scope.thumbnail.indexOf(thumbnail)]);
        }

    }])

问题在于$scope.showVideo = function(scenario)当我致电Data.setData(scenario);时,我收到错误TypeError: Object #<Object> has no method 'setData'

1 个答案:

答案 0 :(得分:1)

.controller('scenariosCtrl', ['$scope', '$http', '$location', 'Data', function($scope, $http, Data)

您在这里缺少$ location服务的一个参数。它应该是

.controller('scenariosCtrl', ['$scope', '$http', '$location', 'Data', function($scope, $http, $location, Data)