javascript - 找不到模块内的 AngularJS 服务

标签 javascript angularjs

我正在尝试模块化我的 AngularJS 应用程序并将一些服务放入模块中。 每次我尝试引用我的服务时,都会出现一条错误消息: 未知提供者:UtilsProvider <- Utils <- benutzerUebersichtController

我的代码是:

var utils = angular.module('Utils',[]);
utils.service('UtilsService', function () {   
    this.CurrentUser = 'Hello World';
});


var verwaltungApp = angular.module('verwaltungApp', ['Utils'])


verwaltungApp.controller('benutzerUebersichtController', ['$scope', '$http', '$filter','Utils', benutzerUebersichtController]);

function benutzerUebersichtController($scope, $http, UtilsService) {
$scope.Test = UtilsService.CurrentUser;
};
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="verwaltungApp">
  <div ng-controller="benutzerUebersichtController">
    <span>{{Test}}</span>
    </div>
  </div>

最佳答案

您没有在 Controller 中正确注入(inject)服务。

这个:

verwaltungApp.controller('benutzerUebersichtController', ['$scope', '$http', '$filter','Utils', benutzerUebersichtController]); 
function benutzerUebersichtController($scope, $http, UtilsService) {
    $scope.Test = UtilsService.CurrentUser;
};

应该是:

verwaltungApp.controller('benutzerUebersichtController', ['$scope', '$http', '$filter','UtilsService', benutzerUebersichtController]);

function benutzerUebersichtController($scope, $http,$filter, UtilsService) {
    $scope.Test = UtilsService.CurrentUser;
};

关于javascript - 找不到模块内的 AngularJS 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30348431/

相关文章:

javascript - 为什么我的指令被调用两次?

javascript - Angular 中不同类型的提供者配置

Javascript 查询或 ORM

javascript - 使用 JavaScript/jQuery 提交表单后调用函数

javascript - jQuery 将两个总数相加

javascript - AngularJS 中同一个表是否可以同时具有 orderBy 和过滤器?

javascript - 为什么 Array.map Array.forEach 异步操作需要 Promise.all?

angularjs - ng-repeat 用于两个不同数组的用法 - angularjs

angularjs - Angular JS 截断文本并添加阅读更多

ruby-on-rails - 返回 Angular 中 Rails API 的搜索结果