0

I am developing my first Angular application. My page contains textbox & html select element. I bind that select element using

 <select data-ng-model="ddlLocation">
      <option value="">--Select--</option>
      <option data-ng-repeat="location in vm.arrLocations" value="{{location.srNo}}">{{location.name}}</option>
 </select>

On controller side, i populate arrLocations as

 this.getLocationsOnSuccess = function (response) {
        //vm.locations = response.data; 
        var arrLocations = new Array();

        var i;
        for (i = 0; i < response.length; i++) {
            vm.locationSrNo = response[i].srNo;
            vm.locationName = response[i].name;

            arrLocations.push({
                srNo: response[i].srNo,
                name: response[i].name
            });  
        }

        vm.arrLocations = arrLocations;
    }

To get default value selected :

vm.ddlLocation = "--Select--";

Now how to get selected value on button click. Is there any alternate approach for CRUD functionality with HTML select in Angular JS?

1
  • u can use ng-change function and pass your model to that Commented Nov 22, 2016 at 10:07

2 Answers 2

1

You should have a look at NgOptions which is a great feature in angularjs. To implement this in your example

<select ng-model="ddlLocation" ng-options="location as location.name for location in vm.arrLocations">
      <option value="">--Select--</option>
 </select>

The location gets bind to the option value and the location.name gets displayed in the option text. The "--Select--" will be selected as default as the ddlLocation is null.

When a option is selected the location value of the option will be the value of the NgModel ddlLocation.

To get the value of the selected option you just use the ng-model value ddlLocation which will hold the selected location object. In your controller you get it as:

$scope.randomFunc = function(){
    var value = $scope.ddlLocation.value;
    var name =  $scope.ddlLocation.name;
 };
Sign up to request clarification or add additional context in comments.

Comments

1

Try this :

this.getLocationsOnSuccess = function (response) {
    //vm.locations = response.data; 
    var arrLocations = new Array();

    var i;
    for (i = 0; i < response.length; i++) {
        vm.locationSrNo = response[i].srNo;
        vm.locationName = response[i].name;

        arrLocations.push({
            srNo: response[i].srNo,
            name: response[i].name
        });  
    }

    vm.arrLocations = arrLocations;

    vm.whatIsMyValue = function() {
        alert(vm.ddLocation);
    }
}

and in your HTML :

<button ng-click="whatIsMyValue()"></button>

It will display your value. As you can see, you can now use it as you want.

5 Comments

I already tried using vm.ddlLocation on controller But it will always return default option ie '--Select--'
well that's because you need to put vm.ddLocation in your select, as you did with other variables.
"you need to put vm.ddLocation in your select " Means??
<select data-ng-model="vm.ddlLocation"> @UmeshAP
Silly mistake while coding. Now code works as expected. Thanks for pointing out.

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.