gpt4 book ai didi

AngularJS - 选择单选按钮时触发

转载 作者:行者123 更新时间:2023-12-03 04:39:19 24 4
gpt4 key购买 nike

我搜索并尝试了很多 ng-xxxx 类型的选项,但找不到那个..我只想在选择单选按钮时调用 Controller 中的某些函数。

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

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

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

最佳答案

至少有两种不同的方法可以调用单选按钮选择的功能:

1) 使用 ng-change 指令:

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

然后,在 Controller 中:

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

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

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

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

但在 Controller 中会有:

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

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

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

关于AngularJS - 选择单选按钮时触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13077320/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com