2016-12-01 4 views
0

[クリア]ボタンをクリックしたときにラジオボタンの状態をチェックするにはどうすればよいですか? NG-繰り返しでラジオボタンリストng-repeatの角ラジオボタンリスト

<label ng-repeat="item in items"> 
    <input type="radio" name="test" ng-model="test" value="{{item.name}}" />{{item.name}} 
</label> 


<a class="undo" ng-click="clear()">Clear</a> 

fiddle

と明確な別のコントローラからの状態をチェックし、それは可能ですか?

+0

[AngularJsの可能性のある重複。クリックしてHTML "ラジオ"入力の選択を解除することはできますか?](http://stackoverflow.com/questions/25443018/angularjs-is-it-possible-to-electelect-html-radio-input-by-click) – Matheno

答えて

0

これはうまく動作します.. !!!

angular.module("myApp", []).controller("Example", ["$scope", function($scope) { 
 
\t $scope.data = {}; 
 
    
 
    $scope.items = [ 
 
    \t {name: 'one'}, 
 
     {name: 'two'}, 
 
     {name: 'three'}, 
 
     {name: 'four'}, 
 
     {name: 'five'}, 
 
     ]; 
 
     
 
     $scope.clear = function(){ 
 
     $scope.data = {}; 
 
     //alert("hello"); 
 
     } 
 
}]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.12/angular.min.js"></script> 
 
<div ng-app="myApp" ng-controller="Example"> 
 

 

 
<label ng-repeat="item in items"> 
 
    <input type="radio" name="test" ng-model="data.test" value="{{item.name}}" />{{item.name}} 
 
</label> 
 
<br> 
 
<a class="undo" ng-click="clear()">Clear</a> 
 

 
</div>

1

ブロードキャストイベントを使用できます。この回答を参照してください:あなたのコントローラで次にhttps://stackoverflow.com/a/19498009/4161992

ご入力ラジオを制御し、次の操作を行います。

.controller('radioController', function($scope){ 
    $scope.$on('triggerClearRadio', function(){ 

     $scope.items.forEach(function (item, i) { 
      item.checked = false; 
     }); 

    }); 
}); 

変更ご入力のhtmlを使用すると、ブロードキャストすることができ、あなたのクリアボタンからではなくng-model="test"

ng-model="item.checked"を使用します次のような 'triggerClearRadio'イベント(名前はそれよりも優れています):

<button type="button" ng-click="$root.$broadcast('triggerClearRadio')>Clear radios</button> 

このフィドルを見てください:http://jsfiddle.net/takuan/aaoub2mg/

+0

(あなたは同じチェックボックスをダブルクリックするとそれをしませんでした)ここで私は別の "クリア"ボタンを持っています、それは "クリア"ボタンをクリックすると動作するはずです – Hunter

+0

あなたが探しているもののボタンhtmlを含むように編集。 –

+0

ここではうまくいきません、私のフィドルを更新してください:( – Hunter

関連する問題