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

@trisweb :+1:

vnaking avatar Oct 15 '15 03:10 vnaking

the same problem sometimes I get this capture1234

other times I get this capture2341

AhmadMansour avatar Oct 19 '15 08:10 AhmadMansour

Agree with @itsmechlark comment

luckylooke avatar Oct 31 '15 19:10 luckylooke

I just came into this problem as well. @itsmechlark has a good point. Framework should not provide a selected option if none is provided within Angular. "? undefined:undefined ?" just seems irresponsible from the framework.

csantosdev avatar Nov 24 '15 02:11 csantosdev

This worked for me.

<select ng-model="foo" ng-init="foo='1' ">
    <option ng-selected="true" value="1">1</option>
    <option value="1">1</option>
    <option value="3">3</option>
</select>

davidnguyen11 avatar Jan 15 '16 07:01 davidnguyen11

Agree with @eyeamaman

JordashTalon avatar Jan 21 '16 22:01 JordashTalon

I run across the same problem too, the option value is generated by php script

<?php while ($row = $options->fetch_array()) { ?>
          <option><?= $row['title']?></option>
<?php }?>

I don't think I can use ng-init="foo='1' to set default value ,cause I don't know the value in advance.

robinclark007 avatar Feb 22 '16 06:02 robinclark007

@robinclark007, it's a workaround (for a not-really-supported usecase), but you could try setting ngInit on the selected option. (You should know the value by then, right ?).

gkalpak avatar Feb 22 '16 10:02 gkalpak

This can be a huge pain with traditional CMS work where you want to rely on the server for a lot of data (and not create a webservis for every bit of it) but still use angular. Here is a small directive to use in the place of ng-model, which will set your model's initial value based on the static markup (value, selected, etc) https://github.com/vespoli/init-model

vespoli avatar Feb 29 '16 23:02 vespoli

I wrote a workaround for this issue in the form of a directive: https://gist.github.com/Tobiaqs/97fba556349281f2db90

Include ts-select-fix in your app dependencies.

<select ng-model="data.kindOfCheese">...</select> becomes <select ts-select-fix ng-model="data.kindOfCheese">...</select>

This directive allows you to:

  • Set a default value for a select
  • Render options using ng-repeat
  • Propagate UI changes to model

Plunker demonstrating the effect: https://plnkr.co/edit/TCaqT1Kr37b1jQyyRSyv?p=preview

EDIT: I just found out that setting the properties in the model as string instead of a number (partially) fixes the problem

Tobiaqs avatar Mar 14 '16 16:03 Tobiaqs

Using 'ng-selected="true' on the option tag I wanted to show as selected worked for me (referring to gsklee's comment from 3rd September 2013)

ashcode09 avatar Mar 23 '16 14:03 ashcode09

I found the answer!!!

html: <select id="buggySelect" ng-model="foo" ng-app > <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> </select>

css: #buggySelect option[value="? undefined:undefined ?"] { display:none; }

js controller: $scope.foo = "2";

this will remove the blank option and point it back to the correct place.

Fierfek avatar Jun 21 '16 07:06 Fierfek

@Fierfek Brilliant. Thanks mate.

Tobiaqs avatar Jun 21 '16 07:06 Tobiaqs

This happens even when the initial value is defined and is a valid option.

HTML

<select ng-model='itemIndex' ng-init='itemIndex = 0'>
    <option value='{{$index}}' ng-repeat='item in lists.items' ng-selected='$parent.itemIndex === $index'>{{$index + 1}}</option>
</select>

I've tested this with and without the ngInit.

Controller

$scope.itemIndex = 0; // Initialized here even though using ng-init in the HTML.

Oddly enough, this same code works later on the page for another <select> element. The only difference is the index variable name and the list of items I repeat over (e.g. `ng-repeat='item in itemList``). Everything is initialized the same way.

c1moore avatar Jun 28 '16 13:06 c1moore

@c1moore, if you think this is a different problem, please open a new issue and provide a runnable demo (e.g. using CodePen, Plnkr etc).

gkalpak avatar Jun 28 '16 16:06 gkalpak