我是 Angular jS 的新手。我正在使用节点服务器在本地部署我的应用程序。在 Chrome 浏览器中遇到这个错误。
PFB 我的代码
Demo.html
<html ng-app="Demo">
<head>
<script type="text/javascript" src="lib/angular.1-2-13.min.js"></script>
<script type="text/javascript" src="lib/angular-route.1-2-13.min.js"> </script>
</head>
<body ng-controller="DemoController">
USA<input type='radio' name='txt1' ng-model="value" ng- change="changeValue(value)" value="USA"> Non-USA<input type='radio' name='txt1' ng-model="value" ng-change="changeValue(value)" value="NUSA">
<select ng-model="selectedItem" ng-options='option.name for option in optionValues'>
</select>
<input type='text' name='txt1' ng-model='txtValue'/>
<input type='button' name='b1' ng-click='takeValue(txtValue)'/>
<a href="#/about">routing</a>
<div ng-view></div>
<script type="text/javascript">
var demo = angular.module( "Demo", ['ngRoute'] );
demo.config(['$routeProvider','$locationProvider', function ($routeProvider,$locationProvider) {
$routeProvider
// Pages
.when("/about", {templateUrl: "/Users/smundhe/Desktop/NewProject/Demo2.html",controller: "Demo2Controller"})
// else 404
.otherwise("/404", {templateUrl: "partials/404.html",controller: "PageCtrl"});
//$locationProvider.html5Mode(true);
}]);
demo.controller(
"DemoController",
function( $scope ) {
$scope.optionValues=[{name:'1'},{name:'2'},{name:'3'}, {name:'4'}];
$scope.takeValue = function(txtValue){
alert(txtValue);
}
$scope.changeValue=function(value){
alert(value);
if(value=="USA"){
$scope.optionValues=[{name:'1'},{name:'2'}, {name:'3'},{name:'4'}];
}
else if(value=="NUSA"){
$scope.optionValues=[{name:'A'},{name:'B'}, {name:'C'},{name:'D'}];
}
}
});
demo.controller(
"Demo2Controller",
function( $scope ) {
$scope.optionValues=[{name:'1'},{name:'2'},{name:'3'},{name:'4'}];
alert('hiii');
});
</script>
服务器.js
// set our port
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var methodOverride = require('method-override');
var port = 3000;
app.use(bodyParser.json());
app.use(bodyParser.json({ type: 'application/vnd.api+json' }));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(methodOverride('X-HTTP-Method-Override'));
app.get('/*', function(req,res)
{
console.log('sending file: ' + req.url);
res.sendfile(__dirname + '/Demo.html');
});
app.listen(port);
有人可以帮我解决这个问题吗?
提前致谢..
最佳答案
您的问题出在这一行:
app.get('/*', function(req,res)
{
console.log('sending file: ' + req.url);
res.sendfile(__dirname + '/Demo.html');
});
这基本上是说对于任何传入的 GET 请求,总是发回 Demo.html
.因此,当您的页面的 <script>
标签请求 angular.min.js
, 给出 Demo.html
,然后浏览器尝试将其作为 javascript 文件运行,抛出“意外的 <
”错误。
相反,您应该做如下事情:
var serveStatic = require('serve-static');
app.use(serveStatic(__dirname, {'index': ['Demo.html']}))
这基本上会为请求的任何文件提供服务,如果没有请求文件(即 /
,则将返回 Demo.html
)。
注意您需要npm install --save serve-static
.
参见 the serve-static docs了解更多信息。
关于angularjs - 未捕获的语法错误 : Unexpected token < angular. 1-2-13.min.js:1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28894544/