0

To begin with, I am an absolute beginner in front-end development, thus please excuse me if the question is too elementary.

The project I am working on, a drop-down list has been defined as:

<div ng-controller="FacetController">
    <div class="spnlocationdrop-container" ng-hide="isservicelocal">
        <select class="spnlocationdrop" ng-model="$parent.locationsearch" ng-options="location for location in locations | sortArray ">
            <option value="">All Cities</option>
        </select>
   </div>
</div>

The list contains cities, out-of which the user has to select one. The value has to be stored, and sent to the backend via an AJAX call.

How do I get the selected value? Until now, I've been doing that using the document.getElementByID().value() function, but since the above list contains no ID, how do I get the value?

0

1 Answer 1

1

ng-model will have the value of the option selected.

Here's a simple working example: Plunker

In my example, data.singleSelect has the value you need so I'm able to output that to the view using {{ data.singleSelect }} though if I wanted to access it in my controller I would do var input = $scope.data.singleSelect and then pass that input variable to the backend via an AJAX call.

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

3 Comments

When I use it in another JS file, it shows "undefined".
What do you mean by "another JS file"? You won't be able to access that variable if the other file (which I'm assuming is another controller) doesn't have access to that scope.
It has the access. I am trying-out the solution you gave, will update in a sec.

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.