简体   繁体   English

Angularjs单选按钮

[英]Angularjs radio buttons

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), 我知道在一些谷歌线程中已经说过这个,但是我仍然找不到将我的无线电输入绑定到模型的正确解决方案(以简洁的方式),

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! 谢谢!

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”。

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 = { };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM