In my MongoDB I have attributes collection, I would like to add a new field "odoo_name" to each attribute and save it.
For example I have 100 attributes and each attribute will have an odoo_name field to update the old database.
Problem : my console.log(odoo_name) shows undefined, it's not seeing the name filled into the input field.
I changed ng-submit function to saveOdooNames(vm.localAttributes) I am getting all data but when I try to access the ng-model still get 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>
This is my controller :
$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 : It works if I save fields one by one, but I need to bulk save these fields.
EDIT :
I updated ng-submit="saveOdooNames(i)"
to ng-submit=vm.localAttributes)"
End Edit
The save button should store all input data in an Array (inputs)
Actually, i was missing that i was getting an array of objects and i was trying to save the array into the inputs array. After I called it without specifying the index which was normal to have "undefined" here is what I needed :
SOLUTION 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
};
But the code above, returns all odoo_name fields even not "touched" or changed so I used the second solution.
SOLUTION 2 :
I used underscore filter and without passing "o" in the function calling directly my model
$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
}
Both solutions work just this one with filter, I could use angular filters for the first one.