2013-12-20 48 views
-1

我試圖將數據從一個控制器傳遞到另一個使用工廠,出於某種原因,我的工廠沒有在角種子中得到識別。這裏是我聲明瞭我廠無法在角度控制器中找到工廠

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; 
     } 
    }; 
}); 

這裏我app.js文件是我使用的工廠數據控制器

.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
.controller('scenariosCtrl', ['$scope', '$http', '$location', 'Data', function($scope, $http, Data) 

您在這裏缺少$ location服務的一個參數。它應該是

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