角度有问题。当我单独运行我的角度代码时,它工作正常,但是当我在本地主机上使用express访问它时,我的角度代码则无法工作。发生的事情是它仅显示html文件。我的服务器代码是:*
var express = require('express'),
app = express();
app.get('/', function(req, res) {
res.sendfile('./app/client/main.html');
});
app.listen(3000, function() {
console.log('I\'m listening...');
})
我的角度控制器代码是
var app = angular.module('tiks', []);
app.controller('mainController', function($scope){
$scope.posts = [];
$scope.newPost = {created_by: '', text: '', create_at: ''};
$scope.post = function(){
$scope.newPost.created_at = Date.now();
$scope.posts.push($scope.newPost);
$scope.newPost = {created_by: '', text: '', created_at: ''};
};
});
和有角度的html代码是
<!--main.html-->
<!doctype html>
<html>
<head>
<title>Tiks</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0/angular.min.js"></script>
<script src="javascripts/tiks.js"></script>
</head>
<body ng-app="tiks">
<div id='main' ng-controller="mainController">
<form ng-Submit="post()">
<input required type="text" placeholder="Your name" ng-model="newPost.created_by" />
<textarea required maxlength="200" rows="3" placeholder="Say something" ng-model="newPost.text"></textarea>
<input class="button" type="submit" value="Chirp!" />
</form>
<div id="post-stream">
<h4>Tiks Feed</h4>
<div class='post' ng-repeat="post in posts | orderBy:'created_at':true" ng-class-odd="'odd'" ng-class-even="'even'">
<p>{{post.created_by}} says {{post.text}} at {{post.created_at}}</p>
</div>
</div>
</div>
</div>
</body>
</html>
请帮忙
您的HTML要求javascripts/tiks.js
在script
标记处,但是您的服务器没有配置服务的路由。
尝试检查已完成的请求,例如使用Google Chrome开发者工具的“网络”标签,请求文件404
时可能会看到错误.js
。
您应该使用express.static来提供文件夹中的文件,而不是像为此那样单独定义特定的资源./app/client/main.html
。
app.use('/', express.static(__dirname + '/app/client/'));
那么您将不得不http://localhost/main.html
访问它。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句