【问题标题】:Misspelled or missing module?拼写错误或缺少模块?
【发布时间】:2016-03-11 07:17:06
【问题描述】:

我有一个使用 AngularJS 的 ASP.NET MVC5 应用程序。在此,我有一个名为 atTheMS 的模块和一个名为 albumService 的服务工厂和 3 个控制器:ListController、EditController 和 DetailsController。服务和控制器都依赖于atTheMS 模块,但我只从albumService 得到这个错误

Uncaught Error: [$injector:nomod] Module 'atTheMS' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.

它说它发生在(anonymous function) albumService.js:27

(function (app) {
var albumService = function ($http, albumApiUrl) {
    var getAll = function () {
        return $http.get(albumApiUrl);
    };
    var getById = function (id) {
        return $http.get(albumApiUrl + id);
    };
    var update = function (album) {
        return $http.put(albumApiUrl + album.Id, album);
    };
    var create = function (album) {
        return $http.post(albumApiUrl, album);
    };
    var destroy = function (album) {
        return $http.delete(albumApiUrl + album.Id);
    };
    return {
        getAll: getAll,
        getById: getById,
        update: update,
        create: create,
        delete: destroy
    };
};
app.factory("albumService", albumService);
}(angular.module("atTheMS")));

使用(angular.module("atTheMS")) 的其他文件不会出现该错误,这是复制我拥有的另一个工厂而生成的,除了单词“album”被单词“book”替换并且模块是“ atTheLibrary”,当我在该页面上时没有错误。我很困惑为什么这会有所不同。任何帮助将不胜感激,谢谢。

根据要求,这是视图:

@section scripts {
    <script src="~/Scripts/angular.js"></script>
    <script src="~/Scripts/angular-route.js"></script>
    <script src="~/Client/Scripts/Album/albumService.js"></script>
    <script src="~/Client/Scripts/Album/atTheMS.js"></script>
    <script src="~/Client/Scripts/Album/ListController.js"></script>
    <script src="~/Client/Scripts/Album/DetailsController.js"></script>
    <script src="~/Client/Scripts/Album/EditController.js"></script>
}

<div data-ng-app="atTheMS">
    <ng-view></ng-view>
</div>
<hr />

【问题讨论】:

  • 你能发布你的 HTML 吗?
  • @Oleander 在编辑中发布,谢谢。
  • 不应该是angular.module("atTheMS", []) btw吗?
  • 不..他已经在 atTheMS.js 中创建了模块
  • 在服务可以使用该模块之前应该加载该模块。 'atTheMS.js' 文件应该在 'albumService.js' 之前加载

标签: javascript angularjs asp.net-mvc-5 factory angular-module


【解决方案1】:

检查您在哪个文件中初始化模块 - atTheMS。必须先初始化/加载 Angular 模块,然后才能在该模块中创建工厂。从您的 HTML 中可以看出,albumService.js 文件是在 atTheMS.js 文件之前加载的。尝试在 atTheMS.js 文件之后加载 albumService.js 文件。

【讨论】:

  • 这是问题所在。谢谢普拉蒂克!
【解决方案2】:

你的 IIFE 格式不正确,基本上你放错了右括号位置)。

应该很简单

代码

(function (app) {

   //inner code should be as is

})(angular.module("atTheMS"));

【讨论】:

  • 不会产生错误Module 'atTheMS' is not available,即使这也是个问题。
猜你喜欢
  • 2011-12-25
  • 2020-01-30
  • 1970-01-01
  • 2020-04-10
  • 1970-01-01
  • 2021-03-12
  • 2018-04-03
  • 2014-10-03
  • 2018-06-22
相关资源
最近更新 更多