AngularJS - 选择单选按钮时触发 [英] AngularJS - Trigger when radio button is selected

查看:42
本文介绍了AngularJS - 选择单选按钮时触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我搜索并尝试了许多 ng-xxxx 类型的选项,但找不到一个..我只想在选择单选按钮时调用控制器中的某些功能.

I searched and tried many ng-xxxx kind of options but couldn't find the one.. I just want to call some function in the controller when radio button is selected.

所以它可能类似于以下..(当然,下面的代码不起作用)

So it might be similar to following..(Of course, below code is not working)

<input type="radio" ng-model="value" value="one" ng-click="checkStuff()"/>

有什么方法可以实现我想要的吗?

Is there any way to achieve what I want?

推荐答案

在单选按钮选择上调用函数的方法至少有 2 种:

There are at least 2 different methods of invoking functions on radio button selection:

1) 使用 ng-change 指令:

<input type="radio" ng-model="value" value="foo" ng-change='newValue(value)'>

然后,在控制器中:

$scope.newValue = function(value) {
     console.log(value);
}

这里是 jsFiddle:http://jsfiddle.net/ZPcSe/5/

Here is the jsFiddle: http://jsfiddle.net/ZPcSe/5/

2) 观察模型的变化.这不需要输入级别的任何特殊内容:

2) Watching the model for changes. This doesn't require anything special on the input level:

<input type="radio" ng-model="value" value="foo">

但在控制器中会有:

$scope.$watch('value', function(value) {
       console.log(value);
 });

还有 jsFiddle:http://jsfiddle.net/vDTRp/2/

And the jsFiddle: http://jsfiddle.net/vDTRp/2/

更多地了解您的用例将有助于提出适当的解决方案.

Knowing more about your the use case would help to propose an adequate solution.

这篇关于AngularJS - 选择单选按钮时触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆