我正在使用mean.js中的CRUD模块向我的网络应用添加数据。
我在添加字符串等方面没有问题,但是我遇到了嵌入数据的问题。
我想要的数据格式如下。
{
"_id":"5559abc02ef1bcdc2e6e6137",
"messages": [
"title":"title of msg",
"msg":"msg content"
],
"created":"2015-05-19T09:30:25.117Z",
"department":"finance",
"name":"accounts receivable"
}
从这个片段我希望能够将数据推送到messages
字段。我已经在如pushing object into array schema in Mongoose这样的帖子中看到过如何做到这一点的想法。
但是我不确定如何用角度来实现这个解决方案。 到目前为止,我有以下内容,它适用于除消息字段之外的所有字段。
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
/**
* Team Schema
*/
var TeamSchema = new Schema({
name: {
type: String, //eg accounts receivable
default: '',
required: 'Please fill Team name',
trim: true
},
department: {
type: String, //eg finance
default: '',
required: 'Please fill department',
trim: true
},
messages: [
{
title: {type: String},
msg: {type: String}
}
],
created: {
type: Date,
default: Date.now
},
user: {
type: Schema.ObjectId,
ref: 'User'
}
});
/**
* Create a Team
*/
var mongoose = require('mongoose'),
errorHandler = require('./errors.server.controller'),
Team = mongoose.model('Team'),
_ = require('lodash');
exports.create = function(req, res) {
var team = new Team(req.body);
team.user = req.user;
team.save(function(err) {
if (err) {
return res.status(400).send({
message: errorHandler.getErrorMessage(err)
});
} else {
res.jsonp(team);
}
});
};
/**
* Angular controller
*/
$scope.create = function() {
// Create new Team object
var team = new Teams ({
name: this.name,
department: this.department,
messages: this.messages.title
//this.messages[0].title
});
// Redirect after save
team.$save(function(response) {
$location.path('teams/' + response._id);
// Clear form fields
$scope.name = '';
$scope.department = '';
$scope.messages.title = '';
}, function(errorResponse) {
$scope.error = errorResponse.data.message;
});
};
角度视图
<form class="form-horizontal" data-ng-submit="create()" novalidate>
<fieldset>
<div class="form-group">
<label class="control-label" for="name">Name</label>
<div class="controls">
<input type="text" data-ng-model="name" id="name" class="form-control" placeholder="Name" required>
</div>
</div>
<div class="form-group">
<label class="control-label" for="department">Department</label>
<div class="controls">
<input type="text" data-ng-model="department" id="department" class="form-control" placeholder="Department" required>
</div>
</div>
<div class="form-group">
<label class="control-label" for="messages">messages</label>
<div class="controls">
<input type="text" data-ng-model="messages.title" id="messages" class="form-control" placeholder="messages">
</div>
</div>
<div class="form-group">
<input type="submit" class="btn btn-default">
</div>
<div data-ng-show="error" class="text-danger">
<strong data-ng-bind="error"></strong>
</div>
</fieldset>
</form>
如何保存邮件标题和邮件,就像保存其他邮件一样?
我还希望让用户能够将自己的字段添加到此消息字段中,以便他们可以将自己的自定义添加到模型中,因此如果解决方案而不是明确提及标题和消息,那将是一件好事。引用each
项。
答案 0 :(得分:1)
您需要在$ scope.create函数中创建一个消息数组,在创建新的团队对象时需要这样:
// Create new Team object
var team = new Teams ({
name: this.name,
department: this.department,
messages: [
{ title: this.messages.title, msg: "" }
]
});
为了添加具有不同标题等的多条消息,我创建了一个范围变量来保存消息(例如$scope.messages = [{title:"",msg:""}]
),然后使用ng-repeat在模板中迭代它以创建多个输入:
<input type="text" data-ng-repeat="message in messages" data-ng-model="message.title" id="messages" class="form-control" placeholder="messages">
然后你可以有一个按钮,用于添加消息&#34;,连接它点击以将另一个{title:"",msg:""}
推送到$ scope.messages数组。最后,您再次更新$ scope.create函数,将该数组添加为messages字段的值:
// Create new Team object
var team = new Teams ({
name: this.name,
department: this.department,
messages: this.messages
});
在清除表单时,只需将$ scope.messages设置回其初始的单项数组值。