angular.js icon indicating copy to clipboard operation
angular.js copied to clipboard

Adding ng-model to predefined Select element adds undefined option

Open OuchMyHead opened this issue 14 years ago • 35 comments

Adding ng-model:"foo" to a prepopulated select element results in an additional "undefined" option being added and removes the effect of select:"select".

The HTML:

<select ng-model="foo" ng-app >
    <option value="1">1</option>
    <option selected="selected" value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    <option value="5">5</option>
</select>

Becomes:

<select ng-model="foo" ng-app >
    <option value="? undefined:undefined ?"></option>
    <option value="1">1</option>
    <option selected="selected" value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    <option value="5">5</option>
</select>

In the example above the first option value="? undefined:undefined ?" is selected instead of the expected option value="2".

Reproduced at http://jsfiddle.net/K8rSu/

OuchMyHead avatar Jun 05 '12 12:06 OuchMyHead

Hello

I have encountered the same issue.

I cannot set a default selected item when using ng-model directive with select element.

A workaround could be to set the value of the foo object in the controller side.

$scope.foo = '2';

lauterry avatar Jul 03 '12 15:07 lauterry

Had the same issue. The work around doesn't work for me.

Urigo avatar Jul 18 '12 14:07 Urigo

Hi Folks,

I am using v1.0.1 and I still encounter the same problem, is there any update about this issue ?

ghost avatar Aug 12 '12 02:08 ghost

it's a feature, not a bug

appetito avatar Oct 01 '12 11:10 appetito

Anyone as a solution ? I have the same bug...

MathRobin avatar Oct 04 '12 11:10 MathRobin

Guys, this is not a bug. The select element is bind to model field, which data is undefined. What value select should display? Yes, undefined. You try to pass data via markup, it is not an Angular way. Just keep your data in JS model, not in HTML markup.

appetito avatar Oct 04 '12 13:10 appetito

Agree with @appetito, this is not a bug as far as I can tell. There is a similar question with an answer on SO: http://stackoverflow.com/questions/12654631/why-does-angularjs-include-an-empty-option-in-select

pkozlowski-opensource avatar Oct 04 '12 16:10 pkozlowski-opensource

As commented by other people this happens when a value is asigned in the model that isn't an option in the select. So it's developers fault. But I also think current behaviour is not good. Data inconsistency happens, regardsless we like it or not, so we have to find ut the best way to deal with it.

Currently if a nonexistant value is assigned Angular adds more inconsistency, it adds a nonexistant option to the select (usually "? string:undefined ?", "? undefined:undefined ?" or "?") so now we have two values that doesn't exist, instead of one. A good behaviour would be adding the nonexistant option to the select or not adding anything. That would be much better than the current behaviour, because of it would keep or mitigate data inconsistency instead of increasing it.

brainsqueezer avatar Oct 07 '12 15:10 brainsqueezer

In my case, I solved it by putting the select element inside the ng-controller related to it.

manlito avatar Oct 28 '12 02:10 manlito

As already commented by many people this works as intended. Closing for now, please reopen if need.

pkozlowski-opensource avatar Nov 24 '12 22:11 pkozlowski-opensource

@pkozlowski-opensource I think there is room for improvement, especially when using together with ngSelected:

<select ng-model="foo" ng-app >
    <option value="1">1</option>
    <option ng-selected="true" value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    <option value="5">5</option>
</select>

In this case I believe it is very explicit that the developer is expecting $scope.foo === 2 after the initial page loading is completed.

gsklee avatar Sep 03 '13 06:09 gsklee

If you really really want to do this, just ng-init your model. You're already doing one thing in a non-Angular way, so you might as well do two.

<select ng-model="foo" ng-app ng-init="foo='2'">

trisweb avatar May 30 '14 22:05 trisweb

I tried everything and only this is working.

 <select ng-init="person='1'" ng-model="person">

electrize avatar Jun 03 '14 06:06 electrize

I have a case where I'm loading data for the selects over $http building the list of options by ng-repeat ... however, when I try to assign the model a value after getting the data over $http, angular still adds a new value saying either number:X or string:X .... I want to set a custom value for the model on building the data (because I don't know that value beforehand)

something like the following snippet doesn't always work!

   Foos.get().success(function(data){
      $scope.foos = data.foos;
      $scope.foo = parseInt(data.foos[0].id);
    }

though on the HTML side I have it say

    <select ng-model="foo">
    <option value="all">all</option>
    <option ng-repeat="foo in foos" ng-value="foo.id">{{ foo.name }}</option>
    </select>

foozee avatar Aug 03 '14 22:08 foozee

Yes even i had this problem, for me it was'nt the assigning the values on load, i used a simple empty value like

<option value="">--Select--</option>

So now the select will by default will have this select option as set in case no value exists and your js checks if value empty will also not be harmed 'cause the value selected is empty and your error is triggered.

Not sure if this is helpful but just my 2 cents

anupsv avatar Nov 26 '14 10:11 anupsv

done!!

wtz avatar May 12 '15 02:05 wtz

@pkozlowski-opensource Same problem, I think ? undefined:undefined ? should not be set as value. When a user choose nothing the ? undefined:undefined ? will be submitted as value.

itsmechlark avatar Jun 05 '15 00:06 itsmechlark

I tried this and it worked,

operator

UthaiahBollera avatar Jul 13 '15 14:07 UthaiahBollera

I think that my problem with all of this is when you are rendering the select via PHP / Twig templates then allowing AngularJS to take over afterwards.

Why can't I just tell AngularJS to select based on the index of the option regardless of value?

PocketNinjaDesign avatar Aug 24 '15 12:08 PocketNinjaDesign

@trisweb , this is work for me.

ZimmerHao avatar Sep 21 '15 07:09 ZimmerHao