2016-12-01 7 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

(당신은 동일한 체크 박스를 두 번 클릭하면 그럴 필요가 없습니다.) 여기에 별도의 "clear"버튼이 있습니다. 'clear'버튼을 클릭하면 작동합니다. – Hunter

+0

찾고있는 버튼 HTML을 포함하도록 편집했습니다. –

+0

그것이 작동하지 않는다면, 내 바이올린을 업데이트 할 수 있습니다. ( – Hunter