什么是MEAN全栈?
schema: {
title: String,
author: String,
body: String,
date: {type: Date, default: Date.now},
category: String,
recommend: Boolean
},
defSortField: "title",
queryFields: ["title", "author"],
schema: [
{
name: "title",
type: String,
sortable: true,
text: "Title"
},
...
]
<tr ng-repeat="item in items">
...
<td><i ng-click="updateDialog(item)"></i>...</td>
</tr>
$scope.updateDialog = function (model) {
...
$scope.editModel = model;
... // 显示该条记录update的modal对话框
}
然后编辑的对话框是这样的:
<div >
<form >
<div ng-repeat="field in appModel.schema" ng-if="field.editable!==false">
... // 里面是各种ui控件,然后属性ng-model="editModel[field.name]"就可以了
</div>
</form>
</div>
add : function($scope, $http, call){
$http.post(update url, $scope.editModel).success(function (data) {
call(data);
}).error(function (data, status) {
call('get data error!');
});
},
router.post('/api/update', function(req, res, next) {
...
var model = new Model(req.body);
Model.update(model, function (err) {
res.json({err : err});
})
});
上面代码中,直接将req.body扔给mongoose的model,然后就可以传给mongoose
Model.statics.update = function (model, call) {
var Model = mongoose.model(modelConf.name);
Model.findOneAndUpdate({_id : model._id}, model, function (err) {
call(err);
})
}
网易云新用户大礼包:https://www.163yun.com/gift
本文来自网易实践者社区,经作者陈志凌授权发布。