0

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.

1
  • Should be using ng-options rather than ng-repeat Commented Jan 17, 2018 at 4:29

3 Answers 3

1

it's wrong. you are matching with option. Change it. I suggest, use the track by $index for no repeat options

<option value="{{option.Value}}" ng-repeat="option in ListOption track by $index">
 {{option.Name}}
</option>
Sign up to request clarification or add additional context in comments.

8 Comments

Doesn't make sense to repeat the <select>
it doesn't repeat the select. it repeats the <options> tag.
No, that's not how ng-repeat works. It repeats the element that you set it on, not it's children
Still not your friend, it still defaults to the first one <option value="? object:null ?"></option>
it does with the {{option.Value}}??
|
1

I think the mistake is that you used the main array to get value instead of iterable option object. Actually, this should work:

<select class="form-control" id="Category" ng-model="Category">
<option ng-repeat="option in ListOption" value="{{option.Value}}">
 {{option.Name}}
</option>
</select>

2 Comments

No. ListOption is the List. he needs to show the option name
Sorry, I just forgot to change ListOption to the option for the Name too.
1

Hope this is helpfull :)

VIEW

<select class="form-control" data-ng-options="list.Name for list in ListOption" data-ng-model="selectedValue">

MODEL

var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope, $http, $log) {
$scope.ListOption = [];
$scope.ListOption.push({ Value: "0", Name: "Car" });
$scope.ListOption.push({ Value: "1", Name: "House" });
$scope.selectedValue = [];

$scope.selectedValue = $scope.ListOption[0];

});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.