greenoldman greenoldman - 1 year ago 44
AngularJS Question

Why does Angular still add unknown selection?

I am pretty new to Angular so I am missing something trivial:

<select ng-model="shop_index">
<option ng-repeat="elem in shops"
ng-selected="{{$first}}"
ng-value="{{$index}}">{{elem.name}}</option>
</select>


When I look at created HTML code it looks fine except for that unknown entry. I add
ng-init="0"
to HTML code -- no change. I init the model value in controller (before fetching the data) -- no change. I init the model after fetching the data -- no change. This unknown option is simply welded :-).

I am not asking how to get rid of it (like adding it manually, but hidden) but how to fix the above code to remove it automatically.

Answer Source

You should use this:

In your controller:

$scope.shop_index = $scope.shops[0];

In your view:

<select ng-model="shop_index" ng-options="elem as elem.name for elem in shops"></select>