由于以下原因,无法实例化模块 myApp:错误:[$injector:nomod] http://errors.angu

Failed to instantiate module myApp due to: Error: [$injector:nomod] http://errors.angularjs.org/1.2.26/$injector/nomod?p0=myApp

本文关键字:错误 injector angu errors http nomod myApp 模块 实例化      更新时间:2023-09-26

我正在尝试学习Angular,并尝试在基于Web的教程之外编写我的第一个控制器。我在JS控制台的标题中不断收到错误,但无法弄清楚发生了什么。该链接将我带到一个页面,该页面引用了包含ngRoute的需求,我已经根据API完成了此操作。当然,我错过了一些东西,但我看不到它是什么。任何帮助将不胜感激,谢谢!

索引.html

<DOCTYPE html>
<html ng-app="myApp">
    <head>
        <title>First Angular Project</title>
        <link href="bootstrap.min.css" rel="stylesheet">
    </head>
    <body>
        <div class="container" ng-controller="FirstController">
            Did the controller load?
        </div>
        <script type="text/javascript" src="angular.min.js"></script>
        <script type="text/javascript" src="angular-route.min.js"></script>
        <script type="text/javascript" src="app.js"></script>
    </body>
</html>

应用.js

(function () {
    var app = angular.module('myApp', ['ngRoute']);
    app.controller('FirstController', function(){
        console.log("FirstController loaded");
    });
})

您的问题是您忘记调用注册模块和控制器的 iife。

(function () {
    var app = angular.module('myApp', ['ngRoute']);
    app.controller('FirstController', function(){
        console.log("FirstController loaded");
    });
})();//<-- here

链接(在错误中)将ngRoute作为提示,只是因为当Angular ng-app指令尝试引导它时,您的模块可能不可用/实例化的原因之一。但在您的情况下,由于您未能将匿名函数调用为 IIFE,它甚至不存在。