0

I've bound newParty.party_type.code model to dropdown. On changing its value from controller it is adding another option with value ? number:2138 ? even if option with value 2138 is already contained in select element.

<select id="partyType" name="partyType" class="ng-valid valid ng-dirty" ng-model="newParty.party_type.code">
   <option value="? number:2138 ?"></option>
   <option value="2138">value 1</option>
   <option value="1689">value 2</option>
</select>

I also tried converting newParty.party_type.code to string in controller. But it still adds new option with value ? string:2138 ?

<select id="partyType" name="partyType" class="ng-valid valid ng-dirty" ng-model="newParty.party_type.code">
   <option value="? string:2138 ?"></option>
   <option value="2138">value 1</option>
   <option value="1689">value 2</option>
</select>

If I select any option manually, model gets correct value. But via controller, it's not working. Why is this happening? How does angular data bindings work? Do I need to be type-aware when dealing with model properties?

EDIT1: Options in select tag are loaded dynamically after page load. (after ng app init) I tried putting some static data and it is working fine. It has something to do with angular not being able to process dynamically loaded data outside angular controllers.

smitrp
  • 1,292
  • 1
  • 12
  • 28
  • 1
    I will suggest you use `ng-options` for the dropdown. – kubuntu Oct 04 '13 at 10:01
  • I just answered the same question here: http://stackoverflow.com/questions/19182961/ng-options-how-to-set-first-select-always-blank/19184617#19184617 – geniuscarrier Oct 04 '13 at 15:30
  • Would like to see your controller code, particularly the `newParty` object and how you are setting the value. – kmdsax Oct 04 '13 at 15:34
  • @geniuscarrier, "how to set first select always blank" is not the question, he is trying to manually select an option from the controller. – kmdsax Oct 04 '13 at 15:36
  • I dont have the option to use `ng-options` because dropdown's options are fetched from other data source than angular models. I tried with static data in above select element and it is working. Here's code [snippet](http://pastebin.com/yHJqTUbC) – smitrp Oct 04 '13 at 21:56

0 Answers0