我已声明我的应用程序并拥有一个 Controller (用于演示目的):
var module = angular.module("app", [])
module.controller("modalCtrl", ["$scope", function ($scope, dataService) {
$scope.printEntity = function () {
console.log(dataService.getEntityArray());
}
}]);
还有一项服务:
module.factory("dataService", function () {
var entityArrayService = [1,2];
return {
getEntityArray: function () {
return entityArrayService;
}
};
});
当我从我的 View 调用 $scope.printEntity
时,我总是被告知 dataService.getEntityArray()
未定义。
我已将服务作为依赖项加载,并在 return 语句之外声明了我的entityArrayService 数组。我到处寻找答案,但没有结果。我的目标是在两个 Controller 之间共享一段数据,但目前我什至无法使用一个 Controller 来检索数据。
最佳答案
该服务未作为依赖项加载。更改此行:
module.controller("modalCtrl", ["$scope", function ($scope, dataService) {
对此:
module.controller("modalCtrl", ["$scope", "dataService", function ($scope, dataService) {
关于javascript - 使用服务传递数据的 undefined variable ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33281662/