无法在coffeescript中加载angularjs模块

Unable to load an angularjs module in coffeescript

本文关键字:加载 angularjs 模块 coffeescript      更新时间:2023-09-26

我试图在CoffeeScript中完整地制作AngularJS应用程序,但当我的主模块加载时,我遇到了以下错误:

Uncaught Error: [$injector:modulerr] Failed to instantiate module app due to:
Error: [$injector:nomod] Module 'app' 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.

我的应用程序很简单:

这是我的index.html:

<html ng-app="app">
<head>
<script type="text/javascript" src="javascripts/angular.js"></script>
<script type="text/javascript" src="javascripts/angular-route.js"></script>
<script type="text/javascript" src="javascripts/coffee-script.js"></script>
<script type="text/coffeescript" src="coffee/app.coffee"></script>
</head>
<body></body>
</html>

我的应用程序(app.coffee)只是:

angular.module 'app', []

我不明白为什么我的"应用程序"模块不可用,因为如果我用它的编译版本(app.js)替换app.coffee,它就会工作。

为什么?问题出在哪里?

angular尝试引导页面后,将编译app.coffee文件。你可以做的是手动引导ng应用程序。

在您的应用程序中。咖啡:

angular.element(document).ready ->
  angular.module 'app', []
  angular.bootstrap document, ['app']

并去掉html标签上的ng-app指令。有关更多信息,请查看Angular Bootstrap。

就我自己而言,我不会在一个高效的应用程序中认真考虑这种设置。我宁愿在服务器端编译coffee脚本。。。