14

I have a DropDown. I'm binding it's value using ng-repeat on Option. I want to Set the selected value using the value field only .

This is My code.

<div ng-controller="myCtrl">
    <select ng-model="selectedItemvalue">
        <option value="">--Select --</option>
        <option ng-repeat="sel in selectables" value="{{sel.value}}">{{sel.label}}</option>
    </select>
    <p>Selected Value is : {{selectedItemvalue}}</p>
</div>

Js

angular.module('myApp', [])

// controller here
.controller('myCtrl', function($scope) {
    $scope.selectables = [
        { label: 'A', value: 1},
        { label:'B', value: 2},
        { label: 'C', value: 3}
    ];

    // this is the model that's used for the data binding in the select directive
    // the default selected item
    //using value, i want to set the selected value
    $scope.selectedItemvalue = "2"; 
})

My Fiddle

As you can see, I want to set selected value uisng only By setting Value.

3 Answers 3

23

You need to use ng-model instead of ng-value to bind it to model.

<select ng-model="selectedItemvalue">

Update: $scope.selectedItem needs to be $scope.selectedItemvalue in controller and then you can use ng-selected to match condition on it

Working Demo

angular
.module('myApp', [])

// controller here
.controller('myCtrl', function($scope) {
    $scope.selectables = [
        { label: 'A', value: 1},
        { label:'B', value: 2},
        { label: 'C', value: 3}
    ];

    // this is the model that's used for the data binding in the select directive
    // the default selected item
    //using value, i want to set the selected value
    $scope.selectedItemvalue = "2"; 
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.10/angular.min.js"></script>
<div ng-controller="myCtrl" ng-app="myApp">
    <select ng-model="selectedItemvalue">
        <option value="">--Select --</option>
        <option ng-repeat="sel in selectables" ng-selected="selectedItemvalue == sel.value" value="{{sel.value}}">{{sel.label}}</option>
    </select>
 <p>Selected Value is : {{selectedItemvalue}}</p>
</div>

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

9 Comments

See the Updated Link. Still Same problem
But Intially , second Itme Is not Selected in your Demo,
you want second item to be selected inititially?
@AmitKumar see the answer and demo now
@AB: yes. It can be any item. Like If I set $scope.selectedItemvalue = "3"; then It will Set Selected Item Whose Value filed Is 3
|
7

You need to basically use the following syntax for your option tag (use ng-selected):

<option ng-repeat="sel in selectables" value="{{sel.value}}" ng-selected="sel.value == selectedItemvalue">{{sel.label}}</option>

Comments

0

If you try

$scope.selectedItemvalue = { label:'B', value: 2};

then it will work.

2 Comments

Hii. No I want to creat object to set Selected Value. I just Want to set using Value field.
then try as <option ng-repeat="sel.value for sel in selectables"

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.