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

时间:2016-06-10 08:47:29

标签: javascript angularjs

我在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控制器中未定义。    我在这里做错了什么?

1 个答案:

答案 0 :(得分:0)

您可以找到解决方案here

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

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