我跟随" MEAN Machine"我试着用Express建立一个Angular,但是当我调用Angular.min.js时,我一直遇到这个错误:
"Uncaught SyntaxError: Unexpected token <"
我现在已经搜索了一个小时左右,并且无法解决问题。
这是我的快递代码:
var express = require('express'),
app = express(),
bodyParser = require('body-parser'),
morgan = require('morgan'),
cors = require('cors'),
path = require('path'),
config = require('./config.js');
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(cors());
app.use(morgan('dev'));
app.use(express.static(__dirname + '/public'));
app.get('*', function(req, res) {
res.sendFile(path.join(__dirname + '/public/app/index.html'));
});
app.listen(config.devPort);
console.log('Running on ' + config.devPort);
这里我的index.html在哪里调用Angular:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Homepage</title>
<base href="/">
</head>
<body ng-app="sample">
<h1>Welcome to the homepage!</h1>
<div ng-controller="ScheduleCtrl as schedule">
<h1 ng-bind="schedule.test"></h1>
</div>
<!-- inject:js -->
<script src="/public/assets/lib/angular/angular.min.js"></script>
<script src="/public/app/app.js"></script>
<script src="/public/app/modules/schedule/schedule.controller.js"></script>
<!-- endinject -->
</body>
</html>
这是app.js文件:
(function() {
'use strict';
angular
.module('sample', []);
})();
这是schedule.controller.js文件:
(function() {
'use strict';
angular
.module('sample'),
.controller('ScheduleCtrl', ScheduleCtrl);
ScheduleCtrl.$inject = [];
function ScheduleCtrl() {
var vm = this;
vm.test = "Hello World!";
};
})();
编辑: 我检查了我的控制台,文件的路径给了我一个304:
GET / 304 1.934 ms - -
GET /public/assets/lib/angular/angular.min.js 304 2.262 ms - -
GET /public/app/app.js 304 2.578 ms - -
GET /public/app/modules/schedule/schedule.controller.js 304 2.855 ms - -
答案 0 :(得分:0)
尝试将脚本放在head标签中而不是body标签(或在body标签之前),在包含脚本之前使用AngularJS语法(ng-app等)
答案 1 :(得分:0)
这可能是错误:
angular
.module('sample'), <-- this comma should not be here
.controller('ScheduleCtrl', ScheduleCtrl);