将 ng-repeat 数据保存在数组中
Save ng-repeat data in Array
在我的 MongoDB 我有属性集合,我想为每个属性添加一个新字段 "odoo_name" 并保存它。
例如我有 100 个属性,每个属性都有一个 odoo_name 字段来更新旧数据库。
问题: 我的 console.log(odoo_name) 显示 undefined,它没有看到填写的名称输入字段。
我将 ng-submit 函数更改为 saveOdooNames(vm.localAttributes) 我正在获取所有数据,但是当我尝试访问 ng-model 时仍然得到 undefined console.log (inputs.odoo_name)
<form ng-submit="saveOdooNames(i)">
<button>Save</button>
<div class="col-md-12">
<ul class="grid">
<li data-ng-repeat="i in vm.localAttributes">
<label>
<div class="attribute-msl-link-label-wrap">
<div class="attribute-msl-link-label">
<span class="attribute-icon">
<i class="glyphicon glyphicon-folder-open"></i>
</span>
<span class="attribute-number">{{ i.number_id }}</span>
<span class="attribute-title" title="{{i.project.name}}">{{ i.name }}</span>
<input ng-model="i.odoo_name" type="text" class="form-control"/>
<a href="javascript:void(0)" class="del-attr" data-id="{{ i._id}}" data-parent="0">
<i class="glyphicon glyphicon-trash"></i>
</a>
</div>
</div>
</label>
</li>
</ul>
</div>
</form>
这是我的控制器:
$scope.saveOdooNames = function (o) {
var inputs = [];
inputs.push(o);
$http({
method: 'POST',
format: 'json',
url: '/api/odoonames',
headers: {
'Content-Type': 'application/json'
},
data: { _id : inputs._id,
odoo_name : inputs.odoo_name }
}).then(function (success) {
console.log('Success ' + JSON.stringify(success));
}, function (error) {
console.error("Error " + JSON.stringify(error));
});
};
PS : 如果我一个一个保存字段就可以,但是我需要批量保存这些字段。
编辑:
我将 ng-submit="saveOdooNames(i)"
更新为 ng-submit=vm.localAttributes)"
结束编辑
保存按钮应将所有输入数据存储在数组(输入)
中
你应该 link 按钮的保存功能,而不是使用 i 作为未定义的参数,因为 ng-repeat 嵌套在 div 的下方,使用 vm.localAttributes 然后您的函数中就会有您的数据。
<form>
<button ng-click="saveOdooNames(vm.localAttributes)">Save</button>
....
$http 方法应该在使用的地方 returned,这是一个承诺。此外,你得到 o
然后将它推入一个数组,然后在不引用数组索引的情况下进行某种对象分配。添加 return 语句后,您可能仍然需要重构在数据参数中分配的数据。希望这对您有所帮助,让您朝着正确的方向前进。
function processNamesRequest(inputs) {
return $http({
method: 'POST',
format: 'json',
url: '/api/odoonames',
headers: {
'Content-Type': 'application/json'
},
data: {
_id: inputs._id,
odoo_name: inputs.odoo_name
}
}).then(function(success) {
console.log('Success ' + JSON.stringify(success));
}, function(error) {
console.error("Error " + JSON.stringify(error));
});
}
$scope.saveOdooNames = function(o) {
//if you dont need an array here
//i would just send `o` as is in the request
var inputs = [];
inputs.push(o);
return processNamesRequest(inputs)
};
/*Refactored: I'd also consider putting all http requests inside of a factory or a service.*/
function processNamesRequest(input) {
return $http({
method: 'POST',
format: 'json',
url: '/api/odoonames',
headers: {
'Content-Type': 'application/json'
},
data: {
_id: input._id,
odoo_name: input.odoo_name
}
})
.then(function(success) {
console.log('Success ' + JSON.stringify(success));
})
.catch(function(error) {
console.error("Error " + JSON.stringify(error));
});
}
$scope.saveOdooNames = function(o) {
return processNamesRequest(o)
};
实际上,我错过了我正在获取一个对象数组并且我试图将该数组保存到输入数组中。在我调用它后没有指定正常的索引 "undefined"
这是我需要的:
解决方案 1:
$scope.saveOdooNames = function(o) {
var data = [];
for (var i = 0; i<o.length; i++) {
data.push({_id: o[i]._id,odoo_name: o[i].odoo_name});
}
console.log(data);
// $http post code
};
但是上面的代码,returns 所有 odoo_name 字段甚至没有 "touched" 或更改,所以我使用了第二种解决方案。
解决方案 2:
我在直接调用我的模型的函数中使用了下划线过滤器并且没有传递 "o"
$scope.saveOdooNames = function () {
var o = _.filter(vm.localAttributes, function (x) {
return x.odoo_name;
});
var data = [];
for (var i = 0; i<o.length; i++) {
data.push({_id: o[i]._id,odoo_name: o[i].odoo_name});
}
console.log(data);
// $http post code
}
这两种解决方案都只适用于带过滤器的解决方案,我可以对第一个使用 angular 过滤器。
在我的 MongoDB 我有属性集合,我想为每个属性添加一个新字段 "odoo_name" 并保存它。
例如我有 100 个属性,每个属性都有一个 odoo_name 字段来更新旧数据库。
问题: 我的 console.log(odoo_name) 显示 undefined,它没有看到填写的名称输入字段。
我将 ng-submit 函数更改为 saveOdooNames(vm.localAttributes) 我正在获取所有数据,但是当我尝试访问 ng-model 时仍然得到 undefined console.log (inputs.odoo_name)
<form ng-submit="saveOdooNames(i)">
<button>Save</button>
<div class="col-md-12">
<ul class="grid">
<li data-ng-repeat="i in vm.localAttributes">
<label>
<div class="attribute-msl-link-label-wrap">
<div class="attribute-msl-link-label">
<span class="attribute-icon">
<i class="glyphicon glyphicon-folder-open"></i>
</span>
<span class="attribute-number">{{ i.number_id }}</span>
<span class="attribute-title" title="{{i.project.name}}">{{ i.name }}</span>
<input ng-model="i.odoo_name" type="text" class="form-control"/>
<a href="javascript:void(0)" class="del-attr" data-id="{{ i._id}}" data-parent="0">
<i class="glyphicon glyphicon-trash"></i>
</a>
</div>
</div>
</label>
</li>
</ul>
</div>
</form>
这是我的控制器:
$scope.saveOdooNames = function (o) {
var inputs = [];
inputs.push(o);
$http({
method: 'POST',
format: 'json',
url: '/api/odoonames',
headers: {
'Content-Type': 'application/json'
},
data: { _id : inputs._id,
odoo_name : inputs.odoo_name }
}).then(function (success) {
console.log('Success ' + JSON.stringify(success));
}, function (error) {
console.error("Error " + JSON.stringify(error));
});
};
PS : 如果我一个一个保存字段就可以,但是我需要批量保存这些字段。
编辑:
我将 ng-submit="saveOdooNames(i)"
更新为 ng-submit=vm.localAttributes)"
结束编辑
保存按钮应将所有输入数据存储在数组(输入)
中你应该 link 按钮的保存功能,而不是使用 i 作为未定义的参数,因为 ng-repeat 嵌套在 div 的下方,使用 vm.localAttributes 然后您的函数中就会有您的数据。
<form>
<button ng-click="saveOdooNames(vm.localAttributes)">Save</button>
....
$http 方法应该在使用的地方 returned,这是一个承诺。此外,你得到 o
然后将它推入一个数组,然后在不引用数组索引的情况下进行某种对象分配。添加 return 语句后,您可能仍然需要重构在数据参数中分配的数据。希望这对您有所帮助,让您朝着正确的方向前进。
function processNamesRequest(inputs) {
return $http({
method: 'POST',
format: 'json',
url: '/api/odoonames',
headers: {
'Content-Type': 'application/json'
},
data: {
_id: inputs._id,
odoo_name: inputs.odoo_name
}
}).then(function(success) {
console.log('Success ' + JSON.stringify(success));
}, function(error) {
console.error("Error " + JSON.stringify(error));
});
}
$scope.saveOdooNames = function(o) {
//if you dont need an array here
//i would just send `o` as is in the request
var inputs = [];
inputs.push(o);
return processNamesRequest(inputs)
};
/*Refactored: I'd also consider putting all http requests inside of a factory or a service.*/
function processNamesRequest(input) {
return $http({
method: 'POST',
format: 'json',
url: '/api/odoonames',
headers: {
'Content-Type': 'application/json'
},
data: {
_id: input._id,
odoo_name: input.odoo_name
}
})
.then(function(success) {
console.log('Success ' + JSON.stringify(success));
})
.catch(function(error) {
console.error("Error " + JSON.stringify(error));
});
}
$scope.saveOdooNames = function(o) {
return processNamesRequest(o)
};
实际上,我错过了我正在获取一个对象数组并且我试图将该数组保存到输入数组中。在我调用它后没有指定正常的索引 "undefined" 这是我需要的:
解决方案 1:
$scope.saveOdooNames = function(o) {
var data = [];
for (var i = 0; i<o.length; i++) {
data.push({_id: o[i]._id,odoo_name: o[i].odoo_name});
}
console.log(data);
// $http post code
};
但是上面的代码,returns 所有 odoo_name 字段甚至没有 "touched" 或更改,所以我使用了第二种解决方案。
解决方案 2:
我在直接调用我的模型的函数中使用了下划线过滤器并且没有传递 "o"
$scope.saveOdooNames = function () {
var o = _.filter(vm.localAttributes, function (x) {
return x.odoo_name;
});
var data = [];
for (var i = 0; i<o.length; i++) {
data.push({_id: o[i]._id,odoo_name: o[i].odoo_name});
}
console.log(data);
// $http post code
}
这两种解决方案都只适用于带过滤器的解决方案,我可以对第一个使用 angular 过滤器。