Triet Pham
Triet Pham

Reputation: 88

Remove first value ? object:null ? in select option Angularjs

I'm having problems with select option in Angularjs This is code in file js

$scope.ListOption = [];
$scope.ListOption.push({ Value: "0", Name: Car });
$scope.ListOption.push({ Value: "1", Name: House });

Here's what the code HTML looks like

<select class="form-control" id="Category" ng-model="Category">

<option ng-repeat="option in ListOption" value="{{option.Value}}">
 {{option.Name}}</option>

</select>

The generated html is

<select class="form-control ng-pristine ng-valid" ng-model="Category" style="padding: 0px;">
<option value="? object:null ?"></option>
<option ng-repeat="option in ListOption" value="0" class="ng-binding ng-scope">Car</option>
<option ng-repeat="option in ListOption" value="1" class="ng-binding ng-scope">House</option>
</select>

I have quite a headache on this issue. Looking forward to having someone help me the other way

Upvotes: 0

Views: 1397

Answers (3)

Vignesh S
Vignesh S

Reputation: 39

As you wanted to mention the first value as default you can use ng-selected in option to set 0 as default. Below mentioned code sample for your reference.

<select class="form-control" id="Category" ng-model="Category">

<option ng-repeat="option in ListOption" value="{{option.Value}}" ng-selected="option.Value == '0'">
 {{option.Name}}</option>

</select>

Upvotes: 0

Dabbas
Dabbas

Reputation: 3238

AngularJs tend to do this (generate extra option elment) when the value of the variable in ng-model doesn't match any of the values of the existing <option>.
You can solve it by setting an initial value (of the same type also), and it's one of the values in your <option> elements .
Or add one <option> element without value and with text Select one inside it. so it'll be selected.

function MyController($scope) {
$scope.Category = "1";
$scope.ListOption = [];
$scope.ListOption.push({ Value: "0", Name: 'Car' });
$scope.ListOption.push({ Value: "1", Name: 'House' });
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<div ng-app ng-controller="MyController">
<select class="form-control" id="Category" ng-model="Category"
  ng-options="item.Value as item.Name for item in ListOption">

</select>

{{Category}}
</div>

Make sure $scope.Category is the same type (not only value) as Value in ListOption variable, otherwise AngularJs will create extra option and select it.

Upvotes: 1

Satpal Tanan
Satpal Tanan

Reputation: 1108

The first option

<option value="? object:null ?"></option>

is generated because the ngModel value Category is not defined and select will match the value of Caegory with its options. Since no option is undefined, it adds a dummy option itself.

To solve this yyu can initialize the ngModel value Category to the first option automatically or add a new option with value as blank string and then initialize Category to blank string. It will also trigger your validation if any. New options would be

$scope.Category = "";
$scope.ListOption = [];
$scope.ListOption.push({ Value: "", Name: "Please Select" });
$scope.ListOption.push({ Value: "0", Name: "Car" });
$scope.ListOption.push({ Value: "1", Name: "House" });

Hope it helps.

Upvotes: 1

Related Questions