javascript - 使用服务传递数据的 undefined variable

标签 javascript html angularjs service controller

我已声明我的应用程序并拥有一个 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/

相关文章:

javascript - Select2 JS 不显示来自 AJAX 的数据

javascript - 如何对 API 调用进行单元测试并断言 React 和 Enzyme 中的状态更改?

javascript - 为什么当我使用表达式时 Angular 不更新 ng-show 状态?

javascript - 学习高级 javascript 的资源,比如为 jquery 和 mootools 编写的东西?

javascript - 我们可以在我们的网站中使用 youtube 视频播放器来播放我们自己的视频(视频不会上传到 youtube 服务器)吗?

javascript - 在滚动 200px 上显示 div,但在单击时隐藏它

html - Flexbox 使水平滚动

html - 将形状从调色板拖到 Konvajs 舞台上

javascript - Bootstrap 和 Angular js 之间的单选按钮冲突

javascript - 如何使用 AngularJS 获取 HTML-INPUT-EMAIL?