4

我是 Angular 的新手,正在尝试捕获选定的单选值,但使用 ng-repeat 时文档不清楚。任何帮助将不胜感激。

<div ng-repeat="item in ed">
  <label for="{{item['code']}}">
    <input ng-change="getPlanTypes()" ng-model="ed" type="radio" id="{{item['code']}}" name="effective_date" value="{{item['code']}}">
    {{item['date']}} </label>
</div>

这是控制器,但我不确定获取所选无线电值的正确方法?

rates.controller('getEffectiveDates', 
    function($scope, $http, $location, myService, localStorageService) {
        myService.effective_dates().then(function(ed) {
        $scope.ed = ed; 
    });

    $scope.getPlanTypes = function() {
        console.log($scope.ed['code']); //Futile attempt that returns undefined
        localStorageService.add('code',$scope.ed['code']);
        $location.path("/plan-types");
    }       
});
4

2 回答 2

3

ng-click="getPlanTypes(item.code)"

在您的控制器中,您可以获得值

$scope.getPlanTypes = function (ed) {
    console.log(ed);
}
于 2013-08-02T18:05:08.517 回答
0

http://jsfiddle.net/2LZpv/

的HTML

<div ng-app="myApp" ng-controller="getEffectiveDates">
<div ng-repeat="item in ed">
  <label for="{{item['code']}}">
      <input ng-click="getPlanTypes(item)" ng-model="ed" type="radio" id="{{item['code']}}" name="effective_date" value="{{item['code']}}"/>
    {{item['date']}} </label>
</div>
</div>

JS

angular.module("myApp",[]).controller('getEffectiveDates', ["$scope", function($scope) {

    $scope.ed = [{code:'1',date:"test date 1"},{code:'2',date:"test date 2"}];

    $scope.getPlanTypes = function(selectedItem) {
        console.log(selectedItem["code"]); //Feeble attempt that returns undefined
    }       
}]);
于 2013-08-02T18:09:02.290 回答