由于以下原因,无法实例化模块myApp失败:错误:[$ injector:nomod] http://errors.angularjs.org/1.2.26/$injector/nomod?p0=myApp [英] Failed to instantiate module myApp due to: Error: [$injector:nomod] http://errors.angularjs.org/1.2.26/$injector/nomod?p0=myApp
问题描述
我正在尝试学习Angular,并尝试在基于Web的教程之外编写我的第一个控制器.我在JS控制台的标题中不断收到错误,无法弄清楚是怎么回事.该链接将我带到一个页面,该页面引用了需要包含ngRoute的页面,该页面已根据API完成.我当然想念什么,但看不到它是什么.任何帮助将不胜感激,谢谢!
I'm trying to learn Angular and attempting to write my first controller outside of a web-based tutorial. I keep getting the error in the title in the JS console and can't figure out what's going on. The link takes me to a page referencing the need to include ngRoute, which I've done according to the API. Surely I'm missing something but I can't see what it is. Any help would be greatly appreciated, thanks!
index.html
index.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>
app.js
(function () {
var app = angular.module('myApp', ['ngRoute']);
app.controller('FirstController', function(){
console.log("FirstController loaded");
});
})
推荐答案
您的问题是您忘记了调用注册模块和控制器的iife.
Your issue is that you forgot to invoke the iife which registers module and controller.
(function () {
var app = angular.module('myApp', ['ngRoute']);
app.controller('FirstController', function(){
console.log("FirstController loaded");
});
})();//<-- here
链接(错误中)仅将ngRoute当作提示来使用,原因是有角度的 ng-app
指令尝试引导它时模块可能不可用/不被实例化的原因之一.但是在您的情况下,由于您未能调用匿名函数作为IIFE,它甚至不存在.
Link (in the error) talks about ngRoute as a hint only for one of the reasons your module might not have been available/instantiated when angular ng-app
directive tried to bootstrap it. But in your case it does not even exists due to your failure to invoke the anonymous function as IIFE.
这篇关于由于以下原因,无法实例化模块myApp失败:错误:[$ injector:nomod] http://errors.angularjs.org/1.2.26/$injector/nomod?p0=myApp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!