0

I use AngularJS and bind a <select> to my model:

<select ng-model="model.something" ng-options="..." >
    <option></option>
</select>

It leaves one empty element on top of dropdown, and then generates other options from my view-model.

These things work great:
1. I choose option from dropdown, it is bound to model.something.
2. I choose empty option, model.something is set to null.
3. I set model.something in another function, the correct option is selected.

What does not work:
- I set model.someting to null in another function, empty option is not selected, the previous value is still selected. How to "tell" Angular to select empty option, if I bind model to null value?

1 Answer 1

4

Change <option></option> to <option value=""></option> and set model.someting to empty string "".

UPDATE

Here is a working JSFiddle: http://jsfiddle.net/DianaNassar/6kLW6/

2
  • Can value be set to null, not ''? When user selects empty, it effectively means, that they want to "delete" their previous decision. I need the value to be null then.
    – Tschareck
    Jun 7, 2014 at 10:28
  • 1
    I use it this way in my applications and it works; empty string, effectively, makes the value "undefined" i.e. not selected. I'll check setting it to "null" or "undefined" in JSFiddle and get back to you. Jun 7, 2014 at 10:35

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.