Call Controller Function by Select Angularjs

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

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

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

Any ideas?

+4
source share
2 answers

Use ng-change with ng-model:

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

Where onSelect () is a method in your area.

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

In your controller:

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

Source: https://habr.com/ru/post/1544827/


All Articles