-1

我正在尝试使用工厂将数据从一个控制器传递到另一个控制器,由于某种原因,我的工厂没有在角种子中得到识别。这是我声明我的工厂的 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'

4

1 回答 1

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

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

.controller('scenariosCtrl', ['$scope', '$http', '$location', 'Data', function($scope, $http, $location, Data)
于 2013-12-20T21:40:22.553 回答