不能获得单选按钮值到控制器

不能获得单选按钮值到控制器

问题描述:

我是新来angularjs.I有一种形式,我有多行单选按钮和textboxes.But我无法获得单选按钮在controller.My代码的价值:不能获得单选按钮值到控制器

HTML:

<tr ng-repeat="consentinfo in listofdata" ng-disabled="abc"> 
    <td><input type="text" class="form-control" ng-model="consentinfo.consentType"></input></td> 
    <td> 
     <input id="radio-1{{$index}}" class="radio-custom" name="radio-group{{$index}}" ng-value="consentinfo.option1" ng-model="radio1" type="radio" ng-click="radio()"> 
     <label for="radio-1{{$index}}" class="radio-custom-label">Yes</label> 
     <input id="radio-2{{$index}}" class="radio-custom" name="radio-group{{$index}}" ng-value="consentinfo.option2" ng-model="radio2" type="radio" ng-click="radio()"> 
     <label for="radio-2{{$index}}" class="radio-custom-label">No</label> 
    </td> 
    <td> 
     <input type="text" ng-model="consentinfo.expDate" class="form-control date" readonly> 
    </td> 
    <td> 
     <input type="text" ng-model="consentinfo.submittedDate" class="form-control date" readonly> 
    </td> 
    <td><input type="text" value="" class="form-control" ng-model="consentinfo.shortDescription"></td> 
    <td><input type="text" value="" class="form-control" ng-model="consentinfo.longDescription"></td> 
</tr> 

.js文件:

var consentManagerApp = angular.module('consentManager', []); 
consentManagerApp.controller('dataOwnerController', ['$scope', function($scope) { 
      $scope.listofdata = [{ 
        "consentType": "Do you consent for us to use your private data for KF Searches", 

        "expDate": "14-12-2017", 
        "submittedDate": "24-04-2017", 
        "option1": "true", 
        "option2": "false", 
        "status": true, 
       }, 
       { 
        "consentType": "Consent to be contacted", 

        "expDate": "10-10-2017", 
        "submittedDate": "12-02-2017", 
        "option1": "true", 
        "option2": "false", 
        "status": true, 
       }, 
       { 
        "consentType": "Consent to be solicited", 

        "expDate": "06-08-2017", 
        "submittedDate": "02-12-2017", 
        "option1": "true", 
        "option2": "false", 
        "status": true, 
       }, 
       { 
        "consentType": "Consent to participate in Trend studies", 

        "expDate": "10-11-2017", 
        "submittedDate": "12-02-2017", 
        "option1": "true", 
        "option2": "false", 
        "status": true, 
       }, 
       { 
        "consentType": "Consent to participate in recruiting practice", 

        "expDate": "10-10-2017", 
        "submittedDate": "12-02-2017", 
        "option1": "true", 
        "option2": "false", 
        "status": true, 
       } 
      ]; 


      $scope.submitConsent = function() { 

       alert('CTY=' + $scope.choices[0] + 'O2=' + $scope.choices[1]); 
       alert('Listdata=' + $scope.radio1 + " " + "O2=" + $scope.radio2); 




      }]); 

     consentManagerApp.filter('unsafe', function($sce) { 
      return $sce.trustAsHtml; 
     }); 

任何一个可以指导我如何我就能拿到。我需要坚持它在数据库中的值。

问候, Prabhash

+0

你能提供一个'小提琴吗? –

+0

请参阅下面的答案。一定要勾选它正确,如果这可以帮助你 –

使用ng-value="{{consentinfo.option1}}",这将解决你的问题不得到单选按钮的值。

你也错过了你的功能$scope.submitConsent的花括号之一。

编辑:有一定的misconcepts您plunker

您需要分配您的ng-model的动态无线电,有点击功能,看看是否可以访问的单选按钮的值。此外,还有其他隐藏的错误,如缺少括号。运行该片段。

<!DOCTYPE html> 
 
<html> 
 
    <head> 
 
     <script data-require="[email protected]" data-semver="1.4.9" src="https://code.angularjs.org/1.4.9/angular.js"></script> 
 
    </head> 
 
    <body ng-app="consentManager"> 
 
     <h1>Hello Plunker!</h1> 
 
     <div ng-controller="dataOwnerController"> 
 
     <table > 
 
      <tbody> 
 
       <tr ng-repeat="consentinfo in listofdata" ng-disabled="abc"> 
 
        <td> 
 
        <input type="text" class="form-control" ng-model="consentinfo.consentType" /> 
 
        </td> 
 
        <td> 
 
        <input id="radio-1{{$index}}" class="radio-custom" name="radio-group{{$index}}" ng-value="{{consentinfo.option1}}" ng-model="radio[consentinfo.expDate]" type="radio" ng-click="callClick(radio[consentinfo.expDate])" /> 
 
        <label for="radio-1{{$index}}" class="radio-custom-label">Yes</label> 
 
        <input id="radio-2{{$index}}" class="radio-custom" name="radio-group{{$index}}" ng-value="{{consentinfo.option2}}" ng-model="radio[consentinfo.expDate]" type="radio" ng-click="callClick(radio[consentinfo.expDate])" /> 
 
        <label for="radio-2{{$index}}" class="radio-custom-label">No</label> 
 
        </td> 
 
        <td> 
 
        <input type="text" ng-model="consentinfo.expDate" class="form-control date" readonly="" /> 
 
        </td> 
 
        <td> 
 
        <input type="text" ng-model="consentinfo.submittedDate" class="form-control date" readonly="" /> 
 
        </td> 
 
        <td> 
 
        <input type="text" value="" class="form-control" ng-model="consentinfo.shortDescription" /> 
 
        </td> 
 
        <td> 
 
        <input type="text" value="" class="form-control" ng-model="consentinfo.longDescription" /> 
 
        </td> 
 
       </tr> 
 
      </tbody> 
 
     </table> 
 
     <button ng-click="submitConsent()">submit</button> 
 
     </div> 
 
    </body> 
 
    <script type="text/javascript"> 
 

 
     var consentManagerApp = angular.module('consentManager', []); 
 
     consentManagerApp.controller('dataOwnerController', ['$scope', function($scope) { 
 
     $scope.radio1; 
 
     $scope.radio2; 
 
        $scope.listofdata = [{ 
 
          "consentType": "Do you consent for us to use your private data for KF Searches", 
 
          "expDate": "14-12-2017", 
 
          "submittedDate": "24-04-2017", 
 
          "option1": "true", 
 
          "option2": "false", 
 
          "status": true, 
 
         }, 
 
         { 
 
          "consentType": "Consent to be contacted", 
 
     
 
          "expDate": "10-10-2017", 
 
          "submittedDate": "12-02-2017", 
 
          "option1": "true", 
 
          "option2": "false", 
 
          "status": true, 
 
         }, 
 
         { 
 
          "consentType": "Consent to be solicited", 
 
     
 
          "expDate": "06-08-2017", 
 
          "submittedDate": "02-12-2017", 
 
          "option1": "true", 
 
          "option2": "false", 
 
          "status": true, 
 
         }, 
 
         { 
 
          "consentType": "Consent to participate in Trend studies", 
 
     
 
          "expDate": "10-11-2017", 
 
          "submittedDate": "12-02-2017", 
 
          "option1": "true", 
 
          "option2": "false", 
 
          "status": true, 
 
         }, 
 
         { 
 
          "consentType": "Consent to participate in recruiting practice", 
 
     
 
          "expDate": "10-10-2017", 
 
          "submittedDate": "12-02-2017", 
 
          "option1": "true", 
 
          "option2": "false", 
 
          "status": true, 
 
         } 
 
        ]; 
 
     
 
        $scope.callClick = function(value){ 
 
        alert(value); 
 
        } 
 
        $scope.submitConsent = function() { 
 
         
 
         // alert('CTY=' + $scope.choices[0] + 'O2=' + $scope.choices[1]); 
 
         // alert('Listdata=' + $scope.radio1 + " " + "O2=" + $scope.radio2); 
 
     
 
     
 
        } 
 
     
 
        }]); 
 
     
 
       consentManagerApp.filter('unsafe', function($sce) { 
 
        return $sce.trustAsHtml; 
 
       }); 
 
    </script> 
 
</html>

+0

我做了更改,我通过consentinfo.option1,consentinfo.option2我的.js文件,我写$ scope.option1和$ scope.option2但它未来定义。 –

+0

@PrabhashMishra你能分享你的表单代码吗? –

+0

要求是第一个文本框的值,两个日期值将来自页面init上的数据库。单选按钮值和用户将能够输入的最后两个文本值。这就是为什么我试图在单选按钮中输入值以及最后两个文本box.I知道如何获得无线电和文本框的静态条目。但在这里它是动态的。我尝试了listofdata [$ index],但它不能与最后两个文本框。你能给我如何重复动态字段是以angularjs访问。 –

<input id="radio-1{{$index}}" class="radio-custom" name="radio-group{{$index}}" ng-value="consentinfo.option1" ng-model="consentinfo.option1" type="radio" ng-click="radio()"> 

取代纳克模态= “收音机1” 到NG-模型= “consentinfo.option1” 纳克模态= “第二广播电台” 到NG-模型=“consentinfo。 option2“

+0

我做了更改,并且我将consentinfo.option1,consentinfo.option2传递给了我的.js文件,我在其中编写$ scope.option1和$ scope.option2,但它未定义。 –