0

I've got a form, which get data from API (input values,select values, which user complete earlier). I try to do "edit" function for this form. User can change form and send it back. The problem is that I need to send only ID's from input fields, but angular sends 'id + name' data. Second problem is that I need to change "status id' when user clicks on submit button.

I've tried to change ng-model, but it didn't help.

API DATA

{
   status: { id: '123', name: 'new'},
   name: 'some_cool_name',
   section: { id: '741', name: 'sectionName';
   presentaion: { id: '365', name: 'some_words'}
}

SERVICE

Object.assign(this, { getAllAwards() { 
  return http.get('myAPI')}.then({ data }) => return data;
}}

CONTROLLER

$scope.allAwards = awards_service.getAllAwards().then((result) => {
  $scope.awards = result;
});

VIEW

<form ng-repeat="awardDraft in awards">
  <input type="text" name="name" ng-model="awardDraft.name">
  <select name="section" ng-model="awardDraft.section.id"
          ng-options="object.id as object.name for object in sections"> 
  </select>
  <input type="text" name="presentation" ng-model="awardDraft.presentation">
</form>

EXPECTED DATA FROM FORM

{
   status: 'someID',
   name: 'someName',
   section: 'sectionID',
   presentaion: 'presentName',
}

WHAT I GET FROM MY FORM

{
   status: { id: 'XXX', name: 'someStatus' },
   name: 'someName',
   section: { id: 'YYY', name: 'sectionName' }
   presentaion: { id: 'ZZZ', name: 'presentName'}
}

I don't understand how in this case ng-model works, because if I send data from usual form, without API loading it works fine. How can I define what to send from specific input and ng-model?

georgeawg
  • 48,608
  • 13
  • 72
  • 95
Oleg Shchegolev
  • 176
  • 1
  • 11
  • So you want delete an element from a json object? If so, take a look at https://stackoverflow.com/questions/742623/deleting-objects-in-javascript (the answer with 4 upvotes) – Reporter Jul 09 '19 at 08:26
  • i need to transform the data which I send back – Oleg Shchegolev Jul 09 '19 at 08:31
  • @reporter I guess I need to understand why ng-model send this data, not only ID's + input values – Oleg Shchegolev Jul 09 '19 at 08:33
  • Then you have to ask this question the developers of angularjs. My suggestion: Create a method that can delete an element. After that create your own object and send it back. – Reporter Jul 09 '19 at 08:38

0 Answers0