0

I'm trying to get the value of a 'select', but it always shows 'undefined'. I would like from the controller, setting the value to 'B' and at the same time get the value of <select>. What am I doing wrong?

<div ng-controller="myCtrl">
    <select ng-model="selectedItemvalue" id='selectedItemvalue' ng-change='myfunction()'>
        <option ng-repeat="sel in selectables" value="{{sel}}">{{sel}}</option>
    </select>

</div>

angular.module('myApp', [])
// controller here
.controller('myCtrl', function($scope) {
  $scope.selectables = [
    'A','B','C'
  ];
  //$scope.selectedItemvalue = "B"; 
  console.log($scope.selectedItemvalue)
})

http://jsfiddle.net/gpehtypy/

I need obtain the value of the select. and then setting the default value 'B'. I need setting the value to 'B' without running ng-change. And I need to get the value of my select, before ng-change.

2 Answers 2

1

Check this

<div ng-controller="myCtrl">
        <select ng-model="selectedItemvalue" id='selectedItemvalue' ng-change='myfunction()'>
            <option ng-selected="sel==selectedItemvalue" ng-repeat="sel in selectables" value="{{sel}}">{{sel}}</option>
        </select>

        <p>Selected Value is : {{selectedItemvalue}}</p>
 </div>

angular.module('myApp', [])

// controller here
.controller('myCtrl', function($scope) {
    $scope.selectables = [
        'A','B','C'
    ];

    $scope.selectedItemvalue = "B"; 
    console.log($scope.selectedItemvalue)
})
Sign up to request clarification or add additional context in comments.

Comments

0

Hey I managed to fix the issues the problem was u needed to execute your code onchange event. So you already have a myfunction() running on ng-change. You just need to put ur $scope inside that function. Hope it Helps.

 $scope.myfunction = function(){
    console.log($scope.selectedItemvalue);
}

http://jsfiddle.net/wb5jxsj4/1/

1 Comment

no..I need setting the value to 'B' without running ng-change. And I need to get the value of my select, before ng-change.

Your Answer

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