0

How show the Value on input type select when loading data from AngularJS $scope? The ng-model is filled by controller with an ID from contactTypes

index.html

<select data-ng-model="item.mediumTypeId"
    data-ng-options="option.name for option in contactTypes track by option.id">
</select>

controller.js

$scope.item.mediumTypeId = 5102;

Option Values after loading index.html

<option value="5101">E-Mail</option>
<option value="5102">Fax</option>
<option value="5103">Phone</option>

The above Code doesn't select the option in the select field after loading index.html

1
  • What are you trying to do? Where is contactTypes in your controller? We need more info before we can you help you Commented Jun 11, 2015 at 6:21

2 Answers 2

1

You need to have:

$scope.item.mediumTypeId = '5102';

As angularjs doesn't convert it to a number automatically.

Sign up to request clarification or add additional context in comments.

Comments

1

If your controller contains something like this:

$scope.contactTypes = [{name: 'one', age: 30 },{ name: 'two', age: 27 },{ name: 'three', age: 50 }];

your template should look like this

<div ng-controller="Test">
  <p>selected item is : {{item.mediumTypeId}}</p>
  <select ng-model="item. mediumTypeId">
    <option ng-repeat="item in contactTypes" value="{{item.age}}">{{item.name}}</option>
  </select>
</div>

I hope this helps

1 Comment

This solution also works fine, it helps to keep the code a bit cleaner

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.