使用下面的代碼,我能夠創建使用通過終端的規則:通過angularjs形式顯示並創建貓鼬模式?
curl -i -X POST -H 'Content-Type: applcation/json' -d '{"name": "ITS ABOUT TIME!", "desc":"another is a db test working well!", "color":"02"}' http://localhost:5000/rules
,並顯示通過{{ruler.name}}和{{ruler.desc}}
...但我不知道如何將此應用於表單。我模糊地看着這個,所以任何方向和幫助將不勝感激。
所有這一切的問題是:如何通過角度形式創建規則。
形式前端:
<form action="/rules" method="post">
<input type="text" placeholder="Name of the new Rule"ng-model="ruler.name"/>
<input type="text" placeholder="small description of this rule" ng-model="ruler.desc"/>
<button type="submit" class="btnCreate" ng-click="creatRule()">CREATE</button>
</form>
模型:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var RuleSchema = new Schema({
name: {type: String, required: true},
desc: {type: String},
color: {type: String}
});
var RuleModel = mongoose.model('Rule', RuleSchema);
exports.index = function (req, res){
return RuleModel.find(function (err, rules) {
if (!err) {
res.jsonp(rules);
} else {
console.log(err);
}
});
}
exports.findById = function (req, res) {
return RuleModel.findById(req.params.id, function (err, rule) {
if (!err) {
res.jsonp(rule);
} else {
console.log(err);
}
});
}
exports.addRule = function (req, res) {
var rule;
rule = new RuleModel({
name: req.body.name,
desc: req.body.desc,
color: req.body.color
});
rule.save(function (err) {
if (!err) {
console.log("created");
} else {
console.log(err);
}
});
return res.send(rule);
}
exports.updateRule = function (req, res) {
return RuleModel.findById(req.params.id, function (err, rule) {
rule.name = req.body.name;
rule.phone = req.body.desc;
rule.color = req.body.color;
rule.save(function (err) {
if (!err) {
console.log("updated");
} else {
console.log(err);
}
res.send(rule);
});
});
}
exports.deleteRule = function (req, res) {
return RuleModel.findById(req.params.id, function (err, rule) {
return rule.remove(function (err) {
if (!err) {
console.log("removed");
return res.send('');
} else {
console.log(err);
}
});
});
}
angularjs:
$scope.createRule = function() {
$http({method: 'jsonp', url: '/rules?callback=JSON_CALLBACK'})
.success(function (data, status, headers, config) {
$scope.rulers = data;
}).
error(function (data, status, headers, config) {
console.log('error in rule creation');
});
}
服務器:
var express = require('express');
var rules = require("./model/ruleModel");
var app = express();
app.get("/", rules.index);
app.get('/rules', rules.index);
app.get('/rules/:id', rules.findById);
app.post('/rules', rules.addRule);
app.put('/rules/:id', rules.updateRule);
app.delete('/rules/:id', rules.deleteRule);
是的我重構了這一點,但也看到您的文章作爲有用的信息,所以我投票它的答案。謝謝! – studiobrain