出错:模块'myApp'不可用!使用AngularJS 1.6.1,Node.js和Express.js

时间:2017-04-27 12:10:34

标签: javascript angularjs node.js express

我是angularJS,Node和Express的初学者,我正在使用angularJS,Node和Express编写一个非常简单的待办事项列表的教程的开头。 我使用的是Node.js v7.9.0,Express.js v4.15.2和AngularJS v1.6.1。 我正在使用nodemon来运行我的server.js。

我一直有3个错误,我不明白为什么:

SyntaxError: expected expression, got '<'[En savoir plus]  app.js:1
SyntaxError: expected expression, got '<'[En savoir plus]  route.js:1
Error: [$injector:modulerr] Failed to instantiate module myApp due to:
[$injector:nomod] Module 'myApp' 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.
http://errors.angularjs.org/1.6.1/$injector/nomod?p0=myApp
minErr/<@https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.js:68:12
module/<@https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.js:2183:17
ensure@https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.js:2107:38
module@https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.js:2181:14
loadModules/<@https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.js:4736:22
forEach@https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.js:357:11
loadModules@https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.js:4720:5
createInjector@https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.js:4642:19
doBootstrap@https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.js:1838:20
bootstrap@https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.js:1859:12
angularInit@https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.js:1744:5
@https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.js:32977:5
trigger@https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.js:3314:5

这是我的文件路径:

-todolist
--node_modules
--public
---views
----home.hmtl
---app.js
---index.html
---route.js
--package.json
--server.js

我的server.js文件:

var express = require('express');
var app = express();
var bodyparser= require('body-parser');
var method = require('method-override');

//application
app.get('*', function(req, res){
    res.sendFile('/public/index.html', {root : __dirname});
});

app.listen(3000);

我的app.js:

angular.module('myApp', ['appRoutes']);

我的路线.js:

angular.module('appRoutes', ['ngRoute'])
.config(function($routeProvider){
   $routeProvider
       .when('/', {
                    templateUrl: '/views/home.html'
                  }
            )
});

我的index.html:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular-route.js"></script>
        <script src="app.js"></script>
        <script src="route.js"></script>
        <title>Une To-Do Liste</title>
    </head>
    <body ng-app="myApp">
        <h1>To-do List</h1>
        <div ng-view></div>
    </body>
</html>

home.html包含一个简单的测试,但无法显示:

<h1>Hello</h1>

目前网站只显示我的index.html字符串'待办事项列表'。 为什么我的app.js和route.js会出现2个语法错误?为什么我的'myApp'模块没有定义?

3 个答案:

答案 0 :(得分:1)

我的app.js:

 angular.module('myApp', ['ngRoute']);

我的路线.js:

 angular.module('myApp')
    .config(function($routeProvider){
       $routeProvider
           .when('/', {
                        templateUrl: '/views/home.html'
                      }
                )
    });
    //with test controller
     angular.module('myApp')
        .controller('TestCtrl',['$scope', function($scope){
           $scope.it = 'works';
        }]);

的index.html

 <!DOCTYPE html>
    <html>
        <head>
            <meta charset="UTF-8">
            <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.js"></script>
            <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular-route.js"></script>
            <script src="app.js"></script>
            <script src="route.js"></script>
            <title>Une To-Do Liste</title>
        </head>
        <body ng-app="myApp">
           <div ng-controller="TestCtrl"> 
              <h1>To-do List</h1>
             it {{it}}
          </div>
        </body>
    </html>

答案 1 :(得分:0)

问题是由于缺少ngRoute模块的包含引起的。请查看AngularJS 1.2 $injector:modulerr

答案 2 :(得分:0)

您需要更改文件的顺序。当您在appRoutes中注入myApp时,应该对其进行定义。

<script src="route.js"></script>
<script src="app.js"></script>

app.js

中定义所有必需的模块
angular.module('appRoutes', ['ngRoute']); //Define module
angular.module('myApp', ['appRoutes']);

route.js 中,检索已定义的appRoutes并添加配置部分

angular.module('appRoutes')
    .config(function($routeProvider){
       $routeProvider
           .when('/', {
                        templateUrl: '/views/home.html'
                      }
                )
    });