0

I have a problem with default value in select element. I have form for create new item and update from table. Here is my code:

Controller:

function MyController($scope){
  $scope.data = {};
  $scope.selectedItem = null; //selected row from table

  this.edit = function(){
    $scope.enableEditation = true; // displayed editation form
    $scope.data = $scope.selectedItem
  }

}

HTML:

<select required ng-model="data.ID" class="form-control">
   <option ng-repeat="option in types" value="{{option.ID}}" ng-selected="option.ID == data.ID">{{option.ID}} - {{option.NAME}}</option>
</select>

Default value is allways empty. Generated html in browser is:

<select class="form-control ng-pristine ng-untouched ng-valid ng-not-empty ng-valid-required" required="" ng-model="data.ID">
  <option selected="selected" value="? number:4 ?"></option>
  <option class="ng-binding ng-scope" value="4" ng-repeat="option in types" ng-selected="option.ID == data.ID">4 - First</option>
  <option class="ng-binding ng-scope" value="12" ng-repeat="option in types" ng-selected="option.ID == data.ID">12 - Last</option>
</select>

Where can be problem? Thanks for advices.

1
  • I updated my question. Commented May 3, 2018 at 8:10

2 Answers 2

1

Your problem is that your adding the value to the options trough

"value={{option.id}}"

This will always result in type conflict when your working with non-string values, since doing so will convert your id to a string, your simplest solution is to use "ng-value" instead and give it as reference:

"ng-value="option.id"
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. It works. But i must change ng-select="data.ID" too.
yea indeed conflict with ngModel
1

You just replace ng-selected="option.ID == data.ID" to ng-selected="data.ID"

<select required ng-model="data.ID" class="form-control">
   <option ng-repeat="option in types" value="{{option.ID}}" ng-selected="data.ID">{{option.ID}} - {{option.NAME}}</option>
</select>

See the document https://docs.angularjs.org/api/ng/directive/ngSelected

1 Comment

It doesnt works. I think problem is in generated html. Angular add this row: <option selected="selected" value="? number:4 ?"></option>

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.