1

I want to set the value of drop down box on my HTML page. I am using angular and I am getting the intended value from REST call.

Other fields like text boxes on my page get the updated value just by saying ng_mode_textId = myNewVal.

It does not work for <select> tag. How to do it?

3
  • are you using ngOptions? Commented Dec 16, 2015 at 20:34
  • I am new to angular. No I am not. Is this a replacement for select tag? I am using plain select tag Commented Dec 16, 2015 at 20:36
  • It's a replacement for the <option> tags, check out this example. Commented Dec 16, 2015 at 20:41

1 Answer 1

4

To specify available options in AngularJS for a select component:

<select ng-options="pair.id as pair.label for pair in myArray" ng-model="selectedId" />

You should set $scope.myArray like [(1, "one"), (2, "two"), (3, "three")], and read/write $scope.selectedId like 1, 2, or 3 for this example to work (make these changes in a controller where this select is contained):

mymodule.controller(['$scope', function($scope) {
    $scope.myArray = [(1, "one"), (2, "two"), (3, "three")];
    $scope.selectedId = 1; //lets give him a default selected value
    $scope.someHowYouWillCallThisFunction = function() {
        console.log("The selected id is: " + $scope.selectedId);
    }
}])

or for key:value pairs from an object:

<select ng-options="key as value for (key, value) in myObject" ng-model="selectedId" />
Sign up to request clarification or add additional context in comments.

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.