如何将所有选中的单选按钮中的数据从模态窗口发送到父控制器?

时间:2017-04-19 14:48:35

标签: javascript html angularjs modal-dialog radio-button

我遇到了这个问题

在mainController中,我声明了一个id列表,名为allUsers,我将它们发送到模态窗口,用一个单选按钮显示,每个单选按钮有不同的名称,以启用多项选择,但都具有相同的ng-model,selectedUsers,即。

一旦我选择了一些显示的用户,我按下OK按钮,触发$ scope.ok函数,在for循环中我想获得我选择的所有用户,并将它们推入列表中,selectedUserIds

但是,一旦我运行代码,我的selectedUsers列表为空(但我选择了3个用户)

所以,最后,有两个可能的问题。

  1. 我的单选按钮代码和从中获取数据是错误的
  2. 我甚至不会将模态窗口中的数据发送到主控制器
  3. 我希望你能帮助我......

    这是代码:

    mainController.js

            $scope.allUsers = [0,1,2,3,4,5,6,7,8,9,11,12,13,14,15,16,17,18,19,20];
            $scope.selectedUserIds = [];
    
            $scope.ok = function() {
                angular.forEach($scope.selectedUsers, function(user) {
                    $scope.selectedUserIds.push(user);
                });
            }
    
    
            $scope.open = function () {
                var modalInstance = $uibModal.open({
                scope: $scope,                  
                templateUrl: '../Popup.html',
                resolve: {
                        allUsers: function() {
                            return $scope.allUsers;
                        }                       
                      }
                });
    
            }
    

    的index.html

    <button ng-click="open()">Share Script</button>
    

    Popup.html

    <div class="modal-header">
        <h3 class="modal-title">Share the Script</h3>
    </div>
    
    
    <div class="modal-body">
    <h4>Check the users who you want to share the selected script!</h4>
    <ul>
        <label ng-repeat="user in allUsers"> 
        <input type="radio" name={{user}} ng-value="{{user}}" ng-model="selectedUsers"
        /> {{user}} user
        </label>
    
    </ul>
    
    
    
    </div>
    
    
    <div class="modal-footer">
         <button class="btn btn-default" type="button" ng-click="ok();$close()">Ok</button>
         <button class="btn btn-warning" type="button" ng-click="$close()">Cancel</button>
    </div>
    

1 个答案:

答案 0 :(得分:0)

我找到了答案,我会在这里发布,如果有人和我有同样的问题......

mainController

    $scope.allUsers = [0,1,2,3,4,5,6,7,8,9,11,12,13,14,15,16,17,18,19,20];
    $scope.selectedUserIds = [];

    $scope.open = function () {
        var modalInstance = $uibModal.open({
        scope: $scope,
        controller: 'modalController'       //<<<<<here is a new controller
        templateUrl: '../Popup.html',
        resolve: {
                allUsers: function() {
                    return $scope.allUsers;
                }                       
              }
        });

       //add here the code to receive the data
       //look bellow, and read the answer

    }

modalController

var myApp = angular.module('myApp', ['ngRoute', 'ui.bootstrap']);
//side note, the main controller belongs to myApp module as well

myApp.controller('modalController', function($scope, $uibModalInstance,
    $modalInstance, users) {

$scope.items = users;
$scope.selectedUsers = [];

$scope.addUsersToList = function(users) {
    //not finished
    //add the users from the selected radio buttons to a list
    //lets call the list: $scope.finalList
}

$scope.ok = function() {
    $uibModalInstance.close($scope.finalList); //here u send back the data
};

$scope.cancel = function() {
    $uibModalInstance.dismiss('cancel');
};

});

然后在mainController中,您需要准备好接收数据

 modalInstance.result.then(function (selectedUsers) {
      $scope.selectedUsers.push(selectedUsers);  //here you get the data from the moduleController
}, function () {
      console.log('Modal dismissed at: ' + new Date());
 });

此代码,您需要添加到上面标记的位置 - &GT; //在这里添加接收数据的代码

然后你可以随心所欲:)