简体   繁体   English

ng-dialog:输入单选按钮值未定义

[英]ng-dialog: input radio button value is undefined

I'm trying to get the radio value button value from ng-dialog but it always got undefined. 我正在尝试从ng-dialog获取单选按钮的值,但始终未定义。

Here is the dialog template: 这是对话框模板:

<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>

Here is the partial js where I start with the dialog: 这是我从对话框开始的部分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);
    }
}

I have tried ng-value as below: 我尝试了ng-value,如下所示:

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

but it still got undefined 但它仍然不确定

Notice that it works when I directly set the value of the radio button like: 请注意,当我直接设置单选按钮的值时,它会起作用:

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

The issue is with ngmodel that's not getting update. 问题是ngmodel无法更新。 You have to initialise ngmodel first in the template. 您必须先在模板中初始化ngmodel。

flag-reasons.html flag-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>

Controller 控制者

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);
         });
     });

Working Fiddle: JSFiddle 工作小提琴: JSFiddle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM