2017-01-15 50 views
0

我試圖從ng對話框中獲取無線電數值按鈕值,但它總是沒有定義。ng對話框:輸入單選按鈕的值是undefined

這裏是對話框模板:

<script type="text/ng-template" id="flag-reasons.html"> 
    <div style="padding:10px;"> 
     <span> 
      What's wrong ? 
     </span> 
     <div class="form-group" ng-repeat="flagreason in flagreasons"> 
      <input type="radio" ng-model="fr" name="frname" value="{{flagreason.id}}"> {{flagreason.title}} 
     </div> 
     <div class="form-group"> 
      <button type="button" ng-click="validFlag()"> 
       Valider 
      </button> 
     </div> 
    </div> 
</script> 

這裏是我開始與對話部分JS:如下

$scope.openFlag = function(){ 
    $scope.dialog = ngDialog.open({ template: 'flag-reasons.html', 
     className: 'ngdialog-theme-default', scope: $scope }); 
    $scope.validFlag = function(){ 
     console.log($scope.fr); 
    } 
} 

我已經試過NG-值:

<input type="radio" ng-model="fr" name="frname" ng-value="flagreason.id"> {{flagreason.title}} 

但它仍然沒有定義

注意,它工作時,我直接設置單選按鈕像值:

<input type="radio" ng-model="fr" name="frname" value="5"> {{flagreason.title}} 

回答

1

的問題是,這不是讓更新ngmodel。您必須首先在模板中初始化ngmodel。

旗reasons.html

<script type="text/ng-template" id="flag-reasons.html"> 
    <div style="padding:10px;"> 
     <span> 
      What's wrong ? 
     </span> 
     <div class="form-group" ng-repeat="flagreason in flagreasons"> 
      <input type="radio" ng-model="cb.fr" name="frname" value="{{flagreason.id}}">{{flagreason.id}} - {{flagreason.title}} 
     </div> 
     <div class="form-group"> 
      <button type="button" ng-click="validFlag()"> 
       Valider 
      </button> 
     </div> 
    </div> 
</script> 

控制器

angular.module("app", ['ngDialog']) 
    .controller('Ctrl',function ($scope, ngDialog) { 
     'use strict'; 
     $scope.cb = {}; 
     $scope.flagreasons = [ 
      {id: 1, title: 'title1'}, 
      {id: 2, title: 'title2'}, 
      {id: 3, title: 'title3'} 
     ]; 

     $scope.openFlag = function(){ 
      $scope.dialog = ngDialog.open({ template: 'flag-reasons.html', 
       className: 'ngdialog-theme-default', scope: $scope }); 
      $scope.validFlag = function(){ 
       console.log($scope.fr); 
      } 
     } 

     $scope.$watch('cb.fr', function (v) { 
     console.log(v); 
     }); 
    }); 

工作小提琴:JSFiddle

+0

我這下面的錯誤與您的代碼:錯誤:[$ rootScope: inprog] $摘要已經在進行中 –

+0

哦。嘗試評論範圍。$應用你將得到的結果? – digit

+0

另一個錯誤:TypeError:ngModel。$ setViewValue不是函數 –