2015-09-08 16 views
8

Tôi có hai nút radio passfail.Cách nhận giá trị của nút radio đã chọn trong angularjs

Cách nhận giá trị của nút radio đã chọn.

<div class="col-md-4"> 
      Result 
      <div class="radio"> 
       <label><input type="radio" name="rdoResult">pass</label> 
      </div> 
      <div class="radio"> 
       <label><input type="radio" name="rdoResult">fail</label> 
      </div> 
     </div> 

Tôi có cần sử dụng mô hình ng hay thứ gì khác không. Trong jquery tôi biết những điều tốt không phải trong angularjs.

+1

hiển thị chúng tôi mã angularjs của bạn ... –

Trả lời

21

Cả hai nên có cùng ng-model với nhau ng-value (có nghĩa là để sử dụng với chọn option s hoặc radio nút), do đó giá trị được lựa chọn sẽ được thay đổi trên result $ phạm vi biến và bạn có thể lấy giá trị bên trong một bộ điều khiển vào mẫu nộp hoặc bấm nút.

Markup

<div class="col-md-4"> 
    Result 
    <div class="radio"> 
     <label> 
      <input ng-model="result" type="radio" name="rdoResult" ng-value="'pass'"> 
       pass 
     </label> 
    </div> 
    <div class="radio"> 
     <label> 
      <input ng-model="result" type="radio" name="rdoResult" ng-value="'fail'"> 
       fail 
     </label> 
    </div> 
</div> 
+0

Awesome..Most cách đơn giản nhất để làm nhiệm vụ này. Cảm ơn – Gaurav123

+0

@ Gaurav123 Vui mừng khi biết điều đó đã giúp .. Cảm ơn :) –

+0

Câu trả lời này vẫn không nói cách chuyển/nhận giá trị "kết quả" trong js khi được thay đổi trong html. Nó chỉ giải thích làm thế nào nó có sẵn trong mẫu html. Câu trả lời dưới đây của Manikanta Reddy hoạt động khi tôi có giá trị là $ scope.result trong js. – CodeTweetie

1

Có bạn phải thêm ng mô hình để có được giá trị của nút radio

<div class="col-md-4"> 
       Result 
       <div class="radio"> 
        <label><input type="radio" ng-model="button.value" name="rdoResult" value="pass">pass</label> 
       </div> 
       <div class="radio"> 
        <label><input type="radio" ng-model="button.value" name="rdoResult" value="fail">fail</label> 
       </div> 
      </div> {{button.value}} 
0

Đây là javascript của bạn

<script> 
    aap=angular.module('myApp',[]) 
    .controller('myCtrl',["$scope",function($scope){ 
     $scope.change='data'; 
     $scope.getVal=function(){ 

     console.log($scope.changedVal); 
     $scope.change=$scope.changedVal; 
     } 
    }]); 

    </script> 

và html của bạn

<body ng-app="myApp"> 
    <h1>{{1+1}}</h1> 
    <div class="col-md-4" ng-controller="myCtrl"> 
      Result 
      <div class="radio"> 
       <label><input type="radio" name="rdoResult" ng-model="changedVal" value="pass" ng-click="getVal()">pass</label> 
      </div> 
      <div class="radio"> 
       <label><input type="radio" name="rdoResult" ng-model="changedVal" value="fail" ng-click="getVal()">fail</label> 
      </div> 
      {{change}} 
     </div></body> 

working demo

Hy vọng đây là những gì bạn đang tìm kiếm.

5

angular.module('resultApp', []).controller('resultCtrl', function($scope) { 
 
    
 
    $scope.result = 'pass'; 
 
    
 
    $scope.submitResult = function(result) { 
 
    
 
    alert(result) 
 
    }; 
 
});
<head> 
 
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
    </head> 
 
<body ng-app='resultApp' ng-controller='resultCtrl'> 
 
    
 
    <div class="col-md-4"> 
 
    Result 
 
    <div class="radio"> 
 
     <label><input type="radio" ng-model='result' ng-value='"pass"' name="rdoResult">pass</label> 
 
    </div> 
 
    <div class="radio"> 
 
     <label><input type="radio" ng-model='result' ng-value='"fail"' name="rdoResult">fail</label> 
 
    </div> 
 
</div> 
 
    {{result}} 
 
    <button ng-click="submitResult(result)">See Result</button> 
 
    
 
    </body>

0

Bên trong Chỉ thị và ng-repeat giải pháp trên sẽ không làm việc

Dưới đây là cách tốt nhất để có được giá trị được lựa chọn bên trong chỉ thị:

Template : '<tr ng-repeat="item in ExistingReportData"><td class="text-center"><input type="radio" name="rbReportData" ng-change="RadioChange(item.ActivityHeaderId)" ng-model="SelectedExistingReportData" ng-value="{{item.ActivityHeaderId}}"/>{{item.ActivityHeaderId}}</td></tr>' 

controller: function ($scope) { 
      $scope.RadioChange = function (s) { 
       $scope.SelectedExistingReportData = s; 
      }; 

      $scope.setWeldDataToGrid = function() { 
       alert($scope.SelectedExistingReportData); 
       // $('.modal').modal('hide'); 
      } 

     } 



'<button class="btn button-main btn-primary btn-sm" id="btnWelderSetTo" ng-click="setWeldDataToGrid()" ><span aria-hidden="true" class="glyphicon glyphicon-save"></span> {{ \'OK\' | translate }}</button>' 
0

Sử dụng dấu chấm (.) trong ng-model đã giải quyết được vấn đề:

Trong HTML của bạn:

<input id="base" type="radio" name="content" ng-model="radio.fileType" ng-value="'base'" ng-click="refreshScreen()"> 

<input id="dynamic" type="radio" name="content" ng-model="radio.fileType" ng-value="'dynamic'" ng-click="refreshScreen()"> 


<button type="button" class="btn btn-default" ng-click="downloadFile(radio.fileType)">Get Template</button> 

Trong điều khiển của bạn:

angular.module('resultApp', []).controller('resultCtrl', function($scope) { 

    $scope.downloadFile= function(result) {  
    alert(result) 
    }; 
}); 
Các vấn đề liên quan