4

I was wondering how I can call a function in my controller depending on the option that is selected in a menu.

For instance, using ng-click, when a is clicked, I can call the function. I want to do something similar upon selection in AngularJS.

<select class="dropdown">  
  <option value="">Menu</option> 
  <option ng-click="open()">Settings</option> // call open() when Settings is selected
</select> 

Any ideas?

1
  • You can use ng-model and $watch for a change in that model. Commented Jun 17, 2014 at 0:08

2 Answers 2

4

Use ng-change with ng-model:

 <select ng-model="model" ng-change="onSelect()" >

Where onSelect() is a method on your scope.

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

1 Comment

Thank you! ng-model is incredibly powerful :)
3
<select ng-model="whatever">
     <option value="settings">Settings</option>
</select>

In your controller:

$scope.$watch('whatever', function(newValue, oldValue) {
     if (newValue == 'settings') { 
         doSomething();
     }
});

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.