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

Can't seem to be able to pass data from one controller to another

问题是我似乎无法将信息从控制器 1 发送到控制器 2...我有 sets/gets 数据的服务,但它不工作。我得到的实际错误是控制器 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 中注入服务 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());
    }
}]);