Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call Controller Function from Select Angularjs

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?

like image 436
kevin Avatar asked Mar 19 '23 07:03

kevin


2 Answers

Use ng-change with ng-model:

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

Where onSelect() is a method on your scope.

like image 88
pixelbits Avatar answered Mar 28 '23 05:03

pixelbits


<select ng-model="whatever">
     <option value="settings">Settings</option>
</select>

In your controller:

$scope.$watch('whatever', function(newValue, oldValue) {
     if (newValue == 'settings') { 
         doSomething();
     }
});
like image 26
dave Avatar answered Mar 28 '23 03:03

dave