2

Something about the following combination of "multiple" and "as" in the options expression is making ng-options set pristine to false when the form first loads. Is this an angular bug? Does anyone know of a workaround?

http://plnkr.co/edit/c9ZIgiiisntkJJBeFFdy

<select multiple name="mySelect" id="mySelect"
      ng-options="option.id as option.name for option in data.availableOptions"
      ng-model="data.selectedOption"></select>
Jason B
  • 41
  • 8
  • This is because it's not really pristine because you have default options selected. What you're looking for is `!myForm.$touched`, which tells you whether the user has interacted with it yet. – CSS Oct 30 '15 at 18:15
  • @CSS while that works, this still seems to be a bug, because setting a default value on any other field, including a select without `multiple` doesn't change `$pristine`. – Claies Oct 30 '15 at 18:21
  • @CSS According to this (http://stackoverflow.com/questions/25025102/angular-difference-between-pristine-dirty-and-touched-untouched) that's not true. I'm interested in whether the value has changed, not whether the select has gained/lost focus. – Jason B Oct 30 '15 at 18:29

2 Answers2

0

When you set data.selectedOption the angular select the options and set pristine to false.

You can set true manually with .$setPristine() method.

Rafael Mih
  • 63
  • 8
0

It was an angular bug.

Yep, that looks like a bug. You don't need select as, but you need multiple. It's got something to do with us checking if the view / model has changed after the options have changed. When that's the case, we call $setViewValue with the new selection, which triggers a call to $setDirty. As a workaround you could add a directive that runs after ngOptions which forces $pristine = true and $dirty = false after the initial loading.

https://github.com/angular/angular.js/issues/13211

Jason B
  • 41
  • 8