Javascript Angularjs 单选按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12597138/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Angularjs radio buttons
提问by alchemication
I know that this has been spoken about in some Google Threads but I still can't find the right solution to bind my radio inputs to a model (in clean'n simple manner),
我知道在一些 Google Threads 中已经谈到了这一点,但我仍然找不到将我的无线电输入绑定到模型的正确解决方案(以简洁的方式),
Currently I have HTML:
目前我有 HTML:
<input ng-model="searchByRma" type="radio" name="search-type">
<input ng-model="searchByDelivery" type="radio" name="search-type">
And in Controller:
在控制器中:
$scope.searchByRma = true;
$scope.searchByDelivery = false;
This does not work (as it would with the checkboxes)...
这不起作用(就像复选框一样)......
Any ideas on how to set the default value on the first radio button without loosing data-binding?
关于如何在不丢失数据绑定的情况下在第一个单选按钮上设置默认值的任何想法?
Thanks!
谢谢!
回答by Tosh
I think you should use same variable with different values in those two radio buttons.
我认为您应该在这两个单选按钮中使用具有不同值的相同变量。
<input ng-model="searchBy" value="Rma" type="radio" name="search-type">
<input ng-model="searchBy" value="Delivery" type="radio" name="search-type">
Then, you should have searchBy
set either "Rma" or "Delivery" depending on
user input.
然后,您应该searchBy
根据用户输入设置“Rma”或“Delivery”。
回答by enriquein
What has worked for me is to set the model variable to { } and that will reset the radio buttons to their default (not selected) state. Of course, this will only work if you have your radio button tags correct as in tosh's answer.
对我有用的是将模型变量设置为 { },这会将单选按钮重置为其默认(未选中)状态。当然,这只有在您的单选按钮标签如 tosh 的答案中一样正确时才有效。
In your case:
在你的情况下:
$scope.searchBy = { };