javascript - Call Controller Function from Select Angularjs - Stack Overflow

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

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?

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?

Share Improve this question asked Jun 17, 2014 at 0:04 kevinkevin 1651 gold badge2 silver badges8 bronze badges 1
  • You can use ng-model and $watch for a change in that model. – Evandro Silva Commented Jun 17, 2014 at 0:08
Add a ment  | 

2 Answers 2

Reset to default 4

Use ng-change with ng-model:

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

Where onSelect() is a method on your scope.

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

In your controller:

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744725025a4590124.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信