我最近一直在教我自己angularjs和node.js,据我所学,我写了这段代码,但是它不起作用,我尝试在控制器中记录一些内容,但没有记录下来,所以也许关于index.html代码出了什么问题的提示:(如果我的代码混乱或其他问题,请提前表示抱歉)
<!Doctype html>
<html ng-app="Test">
<head>
<title>Test</title>
<link rel="stylesheet" type="text/css" href="bootstrap/css/bootstrap.css">
<link rel="stylesheet" type="text/css" href="style.css">
<script type="text/javascript" src="angular.min.js"></script>
<script type="text/javascript" src="angular-route.js"></script>
<script type="text/javascript">
var app= angular.module('Test' , ['ngRoute']);
app.controller('TestController' , function($scope , $http ){
$scope.submitBlog = function(){
$http.post('/blogs' , {blog : $scope.blog}).then(function(){
alert("success");
});
};
});
</script>
</head>
<body ng-app>
<h2>Blog It!</h2>
<br><br>
<div class="test" >
<input type="text" , placeholder="Username">
<br><br>
<input type="password" , placeholder="Password">
<br><br>
<button type="button" > Log In</button>
<br><br>
<a href="">Not a member ? Sign Up!</a>
</div>
<div class="blogfeed">
<h5>Feed :</h5>
<input ng-model="blog" placeholder="Your thoughts?" >
<button type="button" , ng-click="submitBlog()" , class="btn">Submit</button>
</div>
</body>
</html>
服务器代码:
var MongoClient = require('mongodb').MongoClient;
var ObjectId = require('mongodb').ObjectId;
var express = require('express');
var bodyParser = require('body-parser');
var bcrypt = require('bcryptjs');
var app = express();
var db = null;
MongoClient.connect("mongodb://localhost:27017/test" , function(err,dbconn){
if(!err){
console.log("We are connected");
db= dbconn;
}
});
app.use(express.static('public'));
app.use(bodyParser.json());
app.post('/blogs' , function(res,req,next){
db.collection('blogs' , function(err , blogsCollection){
var newBlog= {
text: req.body.blog
};
blogsCollection.insert(newBlog , {w:1} , function(err){
return res.send();
});
});
});
app.listen(3000, function(){
console.log('Example app listening on port 3000');
});
最佳答案
您正在创建一个从未使用过的控制器。
您必须使用ng-controller
指令将控制器绑定到dom元素
您可以像这样将其附加到body
标记
<body ng-app ng-controller="TestController" >
关于javascript - AngularJS代码不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39309612/