繁体   English   中英

Angular.js-无法将选定的单选按钮值传递给控制器

[英]Angularjs - Unable to pass selected radio button value to the controller

我在html中有一个动态生成的单选框的列表:

 **index.html**

    <div id="choiceGraphId1" class="radio-button"  ng-controller="selectGraph1">
      <div ng-repeat="choice1 in choices1">
        <table>
           <label class="labelClass" > <td >   {{choice1.graphChoice1}}  </td> </label> <td>&nbsp </td> <label class="labelClass" >  <td> <input type="radio" ng-model="$parent.selectedGraph1" ng-value="choice1.graphChoice1" name="graphChoice1" required /> </td>  </label></table></div></div>

    Angular Controller:
    app.controller('selectGraph1', function ($scope,$rootScope) {

        $rootScope.selectedGraph1="Choice1";

        $rootScope.choices1 = [{
            graphChoice1: "Choice1"
        }, {
            graphChoice1: "Choice2"
        }
        ];


        alert($rootScope.selectedGraph1);

    });

I want to pass the value of $rootScope.selectedGraph1 to 
PrintReportToPDF controller  :


**index.html**
    <div class="buttonsClass" ng-controller="**PrintReportToPDF**">
                                    <button popover="Click button to open selected date range PDF Report in a new tab"
                                            popover-trigger="mouseenter" type="button" class="btn btn-info"
                                            ng-disabled="dataLoading"
                                            ng-click="PrintReportToPDF()">
                                        <i class="fa fa-file-pdf-o icon"></i>
                                        Preview PDF Report
                                    </button>


...
app.controller('PrintReportToPDF', function($scope, $rootScope, $window, UrlServices , StatsByDatePDF, StatsByDateExcel, Flash) {

    //Preview PDF Report

    alert($rootScope.selectedGraph1);
    alert($rootScope.selectedGraph2);

   $scope.PrintReportToPDF_1_StatisticsByDate = function() {
     ....

我正在尝试访问控制器中的$rootScope.selectedGraph1 ,但在PrintReportToPDF控制器中其值未定义。 我在这里做错了什么?

您可以在这里找到解决方案

var myApp = angular.module('myApp',[]);
myApp.factory('UserService', function() {
  return {
      name : 'anonymous'
  };
});

function MyCtrl($scope, UserService) {
    $scope.name = UserService.name;
}

暂无
暂无

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

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