Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular - ngChange triggers before ngModel for <select>

I found this related topic, where the answer suggests to watch for the model change instead of the change of the select. But in my example the "model" is part of an object inside a list. Is the correct way to initialize $watchfor every object in the list?

Select

 <li data-ng-repeat="obj in main.list track by $index">
    <select data-ng-change="main.changeStatus(obj)" data-ng-model="obj.status">
      <option value="A">A</option>
      <option value="B">B</option>
      <option value="C">C</option>
    </select>
 </li>

changeStatus

I need to update the object or already receive the correct object in the change method.

main.changeStatus = function(obj) {
  var r = new Resource(obj);
  ...
  r.$save({...}, function (savedObj) {
    ...
  });
}

What's the correct way to solve this issue?

thanks

Edit: I guess I didn't ask the correct question.

The posted code doesn't achieve what I am looking for, because the change method gets executed before the model is updated, therefore I send the incorrect/old object to the backend. Now there are a couple of ways to fix that

  • Create a new variable and use it as the model
  • Iterate through the list of objects and $watch every status attribute
  • Another to me unkown way?

Idealy I get the new object to the change method, but I don't know how.

Edit2: I tried to write a fiddle to make it clear, but it works there. I need to look deeper into my code, because the problem seems to be somewhere else.

like image 757
KenavR Avatar asked Oct 20 '22 01:10

KenavR


1 Answers

lets clarify your problem , you have 5 dropdowns , with each you have 3 options a,b,c , and you want to do what in a clear way? if you mean list object in main.list by saying "update the object or already receive the correct object in the change" then your code is ok.

like image 193
katmanco Avatar answered Oct 22 '22 16:10

katmanco