0

In Angular.js - How to check the value of the selected option on a Dropdown?

I have a dropdown menu, and a plus button next to it.

When the button is pressed a function triggers. How do I get "selected" into a var?

({{selected}} works in html, but I want this value in JS)

>  <select ng-options="key as key for (key, prop) in
> vm.schema.properties" ng-model="selected"">
>     </select>  //the dropdown


<p id="plusbutton">  //the add button
<div class="btn btn-info btn-sm" ng-model="selected" ng-click="vm.addMappingField()">
<span class="glyphicon glyphicon-plus"></span>
</div>
</p>

Javascript/Angular:

    function addMappingField() {

       var item = {{selected}}; //Here should be the value of the dropdown

        vm.mappingFields.push({
            destination: "item",
        });
    };

3 Answers 3

1

You can pass selected model as paramter of addMappingField function or you can access it directly in controller like $scope.selected

<div class="btn btn-info btn-sm" ng-click="vm.addMappingField(selected)">


function addMappingField(selected) {

   var item = selected //Here should be the value of the dropdown

    vm.mappingFields.push({
        destination: "item",
    });
};
Sign up to request clarification or add additional context in comments.

2 Comments

that comes out as "selected undefined"
Nevermind i fixed it! thank you. I tried this already but i guess i got the syntax wrong or double defined something. Thanks!
1

First of, you are setting the selected as the NgModel on two different elements, it should only be set on the select tag.

Secondly, connect the selected model to the vm and then just refere it in the addMappingField() method in the controller

View

<select ng-options="key as key for (key, prop) in vm.schema.properties" ng-model="vm.selected"">
</select> 

<p id="plusbutton">
    <div class="btn btn-info btn-sm" ng-click="vm.addMappingField()">
        <span class="glyphicon glyphicon-plus"></span>
    </div>
</p>

Controller

function addMappingField() {

   var item = vm.selected;

    vm.mappingFields.push({
        destination: "item",
    });
};

4 Comments

just add "vm.selected = selected;" to the vm?
@SNT As the vm already is defined, you don't need to set the vm.selected yourself. the ng-model will set the value to the selected option
I understand, thank you Sir. both answers work but i suppose using vm in stead of scope would be a better way to go
@SNT Np, yepp. Either use scope or vm. dont mix the two :)
0

Remove ng-model="selected" in the dom. <div class="btn btn-info btn-sm" ng-click="vm.addMappingField()">

If your using scope as vm makes sure these changes in your code.

 function addMappingField() {

   var item = vm.selected //Here should be the value of the dropdown

    vm.mappingFields.push({
        destination: item,
    });
};

OR if using $scope in your controller change to

var item = $scope.selected

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.