2017-11-18 125 views
-1

我是新來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

請參閱下面的答案。一定要勾選它正確,如果這可以幫助你 –

回答

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訪問。 –

0
<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,但它未定義。 –