问题描述
这是场景,我有几个通过 ng-repeat 重复的列表项.当我们点击提交按钮时,我需要获取所选单选按钮的所有信息(id、名称和描述).我知道我们可以在单选按钮上使用 ng-change 来实现这一点,但它不能满足我的要求.我需要在点击提交按钮时获取它.
Here is the scenario, I have few list items which are repeat via ng-repeat. When we click on the submit button, I need to get all the information of the selected radio button (id, name and description). I know we can achieve this using ng-change on radio buttons, but it does not fulfill my requirement. I need to get it when the submit button is clicked.
这是plunker的链接:
Here is the link to plunker:
http://plnkr.co/edit/YKtWm8kaLOIUfc09ZSCA?p=preview
<form ng-submit="showData()">
<ul>
<li ng-repeat="list in lists">
<input type="radio" name="radiogroup" ng-model="radioMod.value" value="{{list.id}}">
{{list.name}}
<br />
<p>{{list.description}}</p>
</li>
</ul>
<button type="submit">submit</button>
</form>
我没有向控制器添加任何东西...
I have added nothing to the controller...
var myApp = angular.module('myApp', []);
myApp.controller('MainCtrl', function($scope) {
$scope.lists = [
{"id": 1, 'name' : 'radio1', 'description' : 'some text for radio1'},
{"id": 2, 'name' : 'radio2', 'description' : 'some text for radio2'},
{"id": 3, 'name' : 'radio3', 'description' : 'some text for radio3'}
];
});
谢谢.
推荐答案
这是一个更新的 Plunker:
Here is an updated Plunker:
http://plnkr.co/edit/OnHXect37Phij9VAB0ET
您需要使用 ng-value 将值设置为整个列表"对象,不需要额外的标志.
You need to set the value to the whole "list" object with ng-value, no need for an additional flag.
这篇关于按下按钮时获取所有值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!