似乎无法将数据从一个控制器传递到另一个控制器

问题是,我似乎无法将信息从控制器1发送到控制器2 …我有我的服务,设置/获取数据,但它不工作。 我得到的实际错误是,控制器1的dataService.getData不是一个函数…当它在别处工作。

服务1(在自己的文件中)

app.service('dataService', function() { var data, queried; return { setData: function(queryData) { this.data = queryData; this.queried = false; }, getData: function() { this.queried = true; return this.data; } }; }); 

控制器1(发送信息)

 app.controller('MyCtrl', ['$scope', '$location', '$state', function($scope, $location, $state, dataService) { anotherService.functionName(function(err, data) { // do some things here actualService.doesntWork(function(err, data) { if (!err) { var query = {}; query.someField = data.someField; dataService.setData(query); $state.go("go.somewhere.else"); } }); }); }]); 

控制器2(获取信息)

 app.controller('MyCtrl2', ['$scope', '$location', '$state', function($scope, $location, $state, dataService) { $scope.buttonPressed = function() { console.log(dataService.getData()); } }]); 

你没有在MyCtrlMyCtrl2注入service dataService ,确保在使用它之前注入依赖项。

调节器

 app.controller('MyCtrl', ['$scope', '$location', '$state','dataService', //<-added dependency here function($scope, $location, $state, dataService) { anotherService.functionName(function(err, data) { // do some things here actualService.doesntWork(function(err, data) { if (!err) { var query = {}; query.someField = data.someField; dataService.setData(query); $state.go("go.somewhere.else"); } }); }); }]); 

控制器2

 app.controller('MyCtrl2', ['$scope', '$location', '$state','dataService',//<-added dependency here function($scope, $location, $state, dataService) { $scope.buttonPressed = function() { console.log(dataService.getData()); } }]);