【问题标题】:get value from ng-repeat radio buttons从 ng-repeat 单选按钮获取值
【发布时间】:2017-01-06 16:50:40
【问题描述】:

我有 ng-repeat 的单选按钮:

<div ng-repeat="c in currencies">

        <ion-radio   ng-model="checkradio" ng-value='c.code'  >
           {{c.name}} {{c.code}}
    </ion-radio>
          </div>

我打算 $watch 模型和所选单选按钮的值:

$scope.$watch('checkradio', function  () {


      $scope.checkradio = "";

})

我的单选按钮没有任何价值。显然,我做错了什么。我尝试了其他方法,但没有奏效。谁能建议如何从单选按钮中获取值?

【问题讨论】:

  • 你有什么问题?
  • 我无法获取单选按钮的值。
  • 试试这个&lt;ion-radio ng-model="c.isCheck"&gt;。并删除$watch
  • 对不起,它没用。感谢您的帮助。

标签: angularjs radio-button angularjs-ng-repeat


【解决方案1】:

对于绑定,您应该有一个对象而不是变量。 然后在你的控制器中你应该有:

$scope.radios = [{name:"first", code:"1"}, {name:"second", code:"2"}];
$scope.model = { checked: "1" };//check the first element

在您的 HTML 中:

<ion-radio ng-repeat="radio in radios" ng-model="model.checked" ng-value="radio.code">{{radio.name}}</ion-radio>

<div ng-repeat="c in currencies">
    <ion-radio   ng-model="model.checked" ng-value='c.code'  >
       {{c.name}} {{c.code}}
    </ion-radio> 
</div>

你可以找到codepenhere

【讨论】:

    【解决方案2】:

    试试这个,这可能对你有帮助,Here is working fiddle

      <div ng-controller="MyCtrl">
    <div ng-repeat="(key,val) in currencies">
          <input type="radio"  ng-model="$parent.checkradioasd" ng-value='val.code'  >
             {{val.name}}
     </div> <br>
     <div>Selected : {{checkradioasd}}</div>
    

    控制器

      var myApp = angular.module('myApp',[]);
    
     function MyCtrl($scope) {
      $scope.currencies = [{name:'Option 1',code:1},{name:'Option 2',code:2}];
      $scope.checkradio = $scope.currencies[0].code;
    }
    

    【讨论】:

      猜你喜欢
      • 2015-06-27
      • 2015-09-22
      • 2022-11-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多