1

I've got a select element, like this:

<select id="fieldName"
                class="form-control"
                ng-model="condition.type">
            <option value="{{constants.TYPE_SORT_BY_SENDER}}">
                {{locale('Sender')}}
            </option>
            <option value="{{constants.TYPE_SORT_BY_RECIPIENT}}">
                {{locale('Receiver')}}
            </option>
            <option value="{{constants.TYPE_SORT_BY_DATE}}">
                {{locale('Date')}}
            </option>
            <option value="{{constants.TYPE_SORT_BY_ATTACHMENT}}">
                {{locale('Has attachment')}}
            </option>
            <option value="{{constants.TYPE_SORT_BY_SUBJECT}}">
                {{locale('Subject')}}
            </option>
        </select>

constants object is defined in $rootScope on $stateChangeStart event, so when I look on this element using firebug it looks ok.

But the problem is that this select is not selecting the correct option on page load: if I change a constant to the number it works fine.

I don't want to have any magic numbers in my template, but I can't get this select to work.

What should I do here?

k102
  • 7,861
  • 7
  • 49
  • 69
  • your model `condition.type` should be exact same constant so in controller do: `$scope.content.type = constants.TYPE_SORT_BY_RECIPIENT` – maurycy May 07 '15 at 08:59
  • I get `condition.type` from the server and, as I said, if I change a constant to the number its representing - it works... – k102 May 07 '15 at 09:04
  • the constant type and value has to match with the one you defined in rootScope – maurycy May 07 '15 at 09:06
  • they do. I guess that the problem is that `select` is rendered before the rootScope - so there are no values in options when it's rendered – k102 May 07 '15 at 09:47
  • How can the select be rendered before rootScope? and even if it is the two way data binding would take care of that, so i think it's something else, maybe if you caould create plunker with your code that would help – maurycy May 07 '15 at 14:29
  • Possible duplicate of [Angular JS Remove Blank option from Select Option](https://stackoverflow.com/questions/20738953/angular-js-remove-blank-option-from-select-option) – Paul Sweatte May 25 '17 at 23:48

0 Answers0