0

The best method to set the values for your options in your select list, seems to be to use 'track-by' (using a.id as a.name seems to be out-dated) in your ng-options directive. However, when I use 'track by', I am no longer able to set the default value using ng-model.

Take for example, this plunkr: http://plnkr.co/edit/OZCal9ZkCQeqnQJY0WP9?p=preview

  <select ng-model="class.team.leader" ng-options="student.name for student in curTeam.students track by student._id">

Currently, the plunkr correctly sets the default value. However, if you change it to use 'track by', like in the code above, it does not work.

trees_are_great
  • 3,881
  • 3
  • 31
  • 62

1 Answers1

0

track by doesn't work like that. It's used to compare a property on both objects rather than checking for object equality. ie obj1.trackBy === obj2.trackBy rather than obj1 === obj2.

For example, change your default model to:

$scope.class = {
    team: {
       leader: { _id: 2, name: 'Dave', status: 0 }
    }  
};

Without track by this wouldn't work as it's a different object to what's in the students array. But with track by angular will compare the _id properties to decide equality.

noj
  • 6,740
  • 1
  • 25
  • 30
  • I am using 'track by' as used in this answer: http://stackoverflow.com/questions/12139152/how-to-set-value-property-in-angularjs-ng-options It sets the value on the option fields. I feel that you are using trackBy for a different use, but I will read it again as I am probably getting confused. – trees_are_great Oct 15 '14 at 09:13
  • I'm not sure what you're getting at. If your model is just the id property then `a.id as a.name` is not outdated and should be used – noj Oct 15 '14 at 09:19
  • As with the top comment for the selected answer: "Your example doesn't fill value attribute of option. It defines what would be stored in the model of – trees_are_great Oct 15 '14 at 09:28
  • It's not possible to change the value attribute, they are always 0, 1, 2, etc. Why do you need to set the value? – noj Oct 15 '14 at 09:34
  • It is possible to change the value attribute. Just replace the plnkr in the question with the line of code that I have provided. Inspect the html and you will see that the value attribute is equal to the id. – trees_are_great Oct 15 '14 at 09:41
  • 1
    I stand corrected. What I posted [seems to work](http://plnkr.co/edit/jduccryiJFygsM9VuOG7?p=preview). Though if you want the `id as` bit as well that doesn't seem to play nicely. Possible workaround [here](http://gurustop.net/blog/2014/01/28/common-problems-and-solutions-when-using-select-elements-with-angular-js-ng-options-initial-selection/) – noj Oct 15 '14 at 09:58
  • Thanks. This works nicely without having to use the workaround. I had seen that workaround, but didn't want to use it because it seems like a hack. I am not sure why they feel the need for this. I do not get the issues that they claim to have. I was having seemingly similar issues, because of an unrelated issue with asynchronous ajax calls coming back in an unexpected order. Thanks for your help. – trees_are_great Oct 15 '14 at 12:42