AngularJS-选择单选按钮时触发 [英] AngularJS - Trigger when radio button is selected
问题描述
我搜索并尝试了许多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屋!