Angular: 无法使用服务访问控制器中的变量
Angular: Not able to access variables in controller using service
我正在尝试在控制器和函数之间共享一个变量。但是我从控制器那里得到一个错误,这样说:
TypeError: Cannot read property 'getSet' of undefined
我看了很多教程,但不知道哪里错了。
我的服务代码是这样的:
app.service('shareData', function() {
var selected = ["plz", "print", "something"];
var putSet = function(set) {
selected = set;
};
var getSet = function() {
return selected;
};
return {
putSet: putSet,
getSet: getSet
};
});
我可以从我这样定义的函数达到 selected
:
setDisplay = function($scope, $mdDialog, shareData) {
console.log(shareData.getSet()); // this is working
$scope.selected = shareData.getSet();
$scope.hide = function() {
$mdDialog.hide();
};
$scope.cancel = function() {
$mdDialog.cancel();
};
$scope.answer = function(answer) {
$mdDialog.hide(answer);
};
};
我的控制器是这样的:
app.controller('topicController', ['$scope', '$http', '$mdDialog', 'shareData',
function ($scope, $http, $mdDialog, $mdToast, shareData) {
console.log(shareData.getSet()); // NOT WORKING
}]);
您的 topicController
控制器的工厂函数中有额外的 $mdToast
,您需要将其删除。
它不起作用的原因是,目前你在数组中提到了 4 个依赖项,如 ['$scope', '$http', '$mdDialog', 'shareData', function
& 然后你在 DI 数组旁边的函数中使用它的实例。在该函数中,您实际上有 5 个依赖项,其中 $mdToast
是额外的。所以在幕后发生的事情是函数的 $scope
持有 '$scope'
DI 数组的值,同样你从右到左。但是当涉及到 $mdToast
(在控制器函数中)时,它持有 'shareData'
(DI 数组)的值,然后下一个参数 shareData
什么也得不到。
app.controller('topicController', ['$scope', '$http', '$mdDialog', 'shareData',
function ($scope, $http, $mdDialog, shareData) { //<--removed $mdToast
console.log(shareData.getSet());
}
]);
NOTE: You are using DI inline array annotation, so the sequence in which dependency are injected in array, in same sequence you should
inject then in underlying factory function.
我正在尝试在控制器和函数之间共享一个变量。但是我从控制器那里得到一个错误,这样说:
TypeError: Cannot read property 'getSet' of undefined
我看了很多教程,但不知道哪里错了。 我的服务代码是这样的:
app.service('shareData', function() {
var selected = ["plz", "print", "something"];
var putSet = function(set) {
selected = set;
};
var getSet = function() {
return selected;
};
return {
putSet: putSet,
getSet: getSet
};
});
我可以从我这样定义的函数达到 selected
:
setDisplay = function($scope, $mdDialog, shareData) {
console.log(shareData.getSet()); // this is working
$scope.selected = shareData.getSet();
$scope.hide = function() {
$mdDialog.hide();
};
$scope.cancel = function() {
$mdDialog.cancel();
};
$scope.answer = function(answer) {
$mdDialog.hide(answer);
};
};
我的控制器是这样的:
app.controller('topicController', ['$scope', '$http', '$mdDialog', 'shareData',
function ($scope, $http, $mdDialog, $mdToast, shareData) {
console.log(shareData.getSet()); // NOT WORKING
}]);
您的 topicController
控制器的工厂函数中有额外的 $mdToast
,您需要将其删除。
它不起作用的原因是,目前你在数组中提到了 4 个依赖项,如 ['$scope', '$http', '$mdDialog', 'shareData', function
& 然后你在 DI 数组旁边的函数中使用它的实例。在该函数中,您实际上有 5 个依赖项,其中 $mdToast
是额外的。所以在幕后发生的事情是函数的 $scope
持有 '$scope'
DI 数组的值,同样你从右到左。但是当涉及到 $mdToast
(在控制器函数中)时,它持有 'shareData'
(DI 数组)的值,然后下一个参数 shareData
什么也得不到。
app.controller('topicController', ['$scope', '$http', '$mdDialog', 'shareData',
function ($scope, $http, $mdDialog, shareData) { //<--removed $mdToast
console.log(shareData.getSet());
}
]);
NOTE: You are using DI inline array annotation, so the sequence in which dependency are injected in array, in same sequence you should inject then in underlying factory function.