gpt4 book ai didi

javascript - 我如何在 meanjs 中存储 ng-repeat 数组形式的数据?

转载 作者:行者123 更新时间:2023-11-30 16:20:15 25 4
gpt4 key购买 nike

我正在使用来自 meanjs.org 的 MEANJS。尝试存储 deal 的 ng-repeat 数组数据,其中有 dealtypedealprice 作为其下的数组,但无法这样做。我在创建表单中为 ng-repeat 输入标签设置了 addFields,但它不存储数据。这是我的代码。

food.server.model.js

 var FoodSchema = new Schema({
created: {
type: Date,
default: Date.now
},
name: {
type: String,
default: '',
required: 'Please fill Food name',
trim: true
},
deal: [{
dealtype: {
type: String,
default: '',
trim: true
},
dealprice: {
type: String,
default: '',
trim: true
}
}],
user: {
type: Schema.ObjectId,
ref: 'User'
}
});

foods.client.controller.js

// initial array setup

var deal = [ { dealtype: '',dealprice: '' }, {dealtype: '',dealprice: '' } ];

$scope.food = {};
$scope.food.deal = deal;
$scope.addItem = function() {
$scope.food.deal.push({
dealtype: '',
dealprice: ''
});
};

// Create new Food
$scope.create = function (isValid) {
$scope.error = null;
if (!isValid) {
$scope.$broadcast('show-errors-check-validity', 'foodForm');
return false;
}

// Create new Food object
var food = new Foods({
name: this.name,
deal:[{
dealtype: this.dealtype,
dealprice: this.dealprice,
}],
});

// Redirect after save
food.$save(function (response) {
$location.path('foods/' + response._id);

// Clear form fields
$scope.name = '';
$scope.dealtype = '';
$scope.dealprice = '';
}, function (errorResponse) {
$scope.error = errorResponse.data.message;
});
};

创建-food.client.view.html

<form name="foodForm" class="form-horizontal" ng-submit="create(foodForm.$valid)" novalidate>
<fieldset>
<div class="col-md-12">
<md-input-container flex="">
<label >Food Name</label>
<input type="text" data-ng-model="name" id="name" required>
</md-input-container>
</div>
<div ng-repeat="de in food.deal">
<div class="col-md-6">
<md-input-container class="">
<label class="" for="dealtype">Dealtype</label>
<input type="text" data-ng-model="de.dealtype" id="dealtype" >
</md-input-container>
</div>
<div class="col-md-6">
<md-input-container class="">
<label class="" for="dealprice">Dealprice</label>
<input type="text" data-ng-model="de.dealprice" id="dealprice" >
</md-input-container>
</div>
</div>
<a href ng:click="addItem()" class="btn btn-small">add item</a>
<button class="md-primary md-raised width-100 md-whiteframe-5dp" type="submit">Create Food</button>

<div ng-show="error" class="text-danger">
<strong ng-bind="error"></strong>
</div>
</fieldset>
</form>

最佳答案

尝试在没有嵌入式数组的情况下设置您的架构,并在没有交易的情况下发送。如果可行,您可能需要使用子文档设置架构。

如果您想将交易属性设置为子文档,则需要遵循 this .您可以单独为交易定义一个模式。像这样:

var dealSchema = new Schema({ {
dealtype: {
type: String,
default: '',
trim: true
},
dealprice: {
type: String,
default: '',
trim: true
}
} });

var FoodSchema = new Schema({
...,
deal: [dealSchema]
})

关于javascript - 我如何在 meanjs 中存储 ng-repeat 数组形式的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34849339/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com