【问题标题】:how to pass back the data from dialog to controller in angularjs?如何将数据从对话框传回angularjs中的控制器?
【发布时间】:2018-09-24 13:27:17
【问题描述】:

下面是我的第一个控制器

.controller('configManagementCtrl', ['$scope', 'deConfigService', 'ngDialog', '$state', 'notificationService',
    function ($scope, deConfigService, ngDialog, $state, notificationService) {

        $scope.loadDetails = function () {
            ....
        };

        $scope.openModal = function () {
            var newClassDialog = ngDialog.open({
                template: 'views/modals/newClassModal.html',
                closeByEscape: false,
                controller: 'newClassController',
                className: 'ngdialog-theme-default',
                width: 600
            });

            newClassDialog.closePromise.then(function (data) {
                console.log(data);
                if (data.passBackData.value === 2) {
                    $scope.loadDetails();
                    // $state.go('app.config', {}, {reload: true, inherit: false});
                    // $scope.loadDetails();
                }
            });
        };

    }])

在我的第二个控制器中,我试图向我的父控制器发送一些数据,如下所示

.controller('newClassController', ['$scope', 'ngDialog', 'deConfigService', 'notificationService',
    function ($scope, ngDialog, deConfigService, notificationService) {
        $scope.classObj = {};
        var passBackData = [];
        $scope.cancel = function () {
            passBackData.push({'closeVal': 1});
            console.log(passBackData);
            ngDialog.close(1, passBackData);
        };
        $scope.create = function (isFormValid) {
            if (isFormValid) {
                $scope.classObj.added_dt = (new Date()).toISOString();
                $scope.classObj.class_id = 0;
                deConfigService.createClass($scope.classObj, function (response) {
                    if (response.data) {
                        console.log(response.data);
                        passBackData.push(response.data.data);
                        notificationService.addSuccess('Class created successfully');
                    }
                    else {
                        notificationService.addError('Error!! Please try later');
                    }
                });
                ngDialog.close(1, 2);
            }
        };
    }])

下面是 ngdialog html。它有 2 个文本框,可以将数据发送到我的第二个控制器,但无法将 response 发送回第一个控制器

   <form ng-submit="create(form.$valid)" name="form" novalidate="">
                <div class="form-flex ng-pristine ng-invalid ng-touched">
                    <div class="form-tile">
                        <label>Class name </label>
                        <input type="text" ng-model="classObj.name" name="form.name" placeholder="Enter the name of your class" required>

                        <label>Class description</label>
                        <textarea ng-model="classObj.description" name="form.description" placeholder="Enter a short description" rows="5" required></textarea>
                    </div>
                </div>

                <button type="submit" ng-click="submittedForm = true;" ng-disabled="form.$invalid" class="mat-raised-button-blue"> Create </button>
                <button class="mat-raised-button" style="float:right; width:155px" ng-click="cancel();"> Cancel </button>
        </form>

正在将一些对象推送到数组并尝试发送但无法从父控制器接收。

哪里做错了?

【问题讨论】:

  • 您能否将您的代码放入 plunkr 或提供更多信息(如两个控制器和 HTML 模板)?很难看出你想在这里做什么......
  • @BoDeX 请检查更新后的问题
  • 根据the documentation close() 应该使用单个参数调用,该参数是要传回的值。所以你不应该打电话给ngDialog.close(passBackData);吗?
  • 也试过了,但不接受
  • 我是 Angular 的新手,所以我的水平很低,如果我的评论无关紧要或愚蠢,请忽略我的评论,您是否有可能在您的第二个控制器中遗漏了“$state”?跨度>

标签: javascript angularjs ng-dialog


【解决方案1】:

仔细阅读文档后,您似乎需要调用 .close() 传递对话框的 id 和从对话框控制器返回的值。在您的父控制器中,传递回您的closePromise 回调的对象具有idvalue 属性。您需要通过value 属性(即data.value.whateverYouAreReturning)获取您传递回来的任何内容。这是一个简单的示例,它返回一个具有单个字符串属性的对象。

angular.module('app', ['ngDialog'])
  .controller('ctrl', ($scope, ngDialog) => {
    $scope.returnedValue = "";
    $scope.openModal = function() {
      var newClassDialog = ngDialog.open({
        template: 'dialogTemplate',
        closeByEscape: false,
        controller: 'dialogCtrl',
        className: 'ngdialog-theme-default',
        width: 600
      });

      newClassDialog.closePromise.then(function(data) {
          $scope.returnedValue = data.value.result;
      });
    };
  })
  .controller('dialogCtrl', ($scope, ngDialog) => {
    var id = ngDialog.getOpenDialogs()[0];
    $scope.returnValue = "";
    $scope.close = () => {
      ngDialog.close(id, { result: $scope.returnValue });
    };
  });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.7.2/angular.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/ng-dialog/1.4.0/js/ngDialog.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/ng-dialog/1.4.0/css/ngDialog.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/ng-dialog/1.4.0/css/ngDialog-theme-default.min.css">
<div ng-app="app" ng-controller="ctrl">
  <button ng-click="openModal()">Open Modal</button>
  
  <p>Returned from dialog: {{ returnedValue }}</p>  

  <script type="text/ng-template" id="dialogTemplate">
    <h1>ngDialog Sample</h1>
    <p>
      <label>Enter a value to return: </label>
      <input type="text" ng-model="returnValue" />
    </p>
    <p><button ng-click="close()">Close</button></p>
  </script>
</div>

【讨论】:

    【解决方案2】:

    这可能有效(除非您可以共享一个 plunker,否则无法对其进行测试):

    .controller('configManagementCtrl', ['$scope', 'deConfigService', 'ngDialog', '$state', 'notificationService', 函数 ($scope, deConfigService, ngDialog, $state, notificationService) {

        $scope.loadDetails = function () {
            ....
        };
    
        $scope.openModal = function () {
            $scope.newClassDialog = ngDialog.open({
                template: 'views/modals/newClassModal.html',
                closeByEscape: false,
                controller: 'newClassController',
                className: 'ngdialog-theme-default',
                width: 600
            });
    
            $scope.newClassDialog.closePromise.then(function (data) {
                console.log(data);
                if (data.passBackData.value === 2) {
                    $scope.loadDetails();
                    // $state.go('app.config', {}, {reload: true, inherit: false});
                    // $scope.loadDetails();
                }
            });
        };
    
    }])
    

    在另一个控制器中:

    .controller('newClassController', ['$scope', 'ngDialog', 'deConfigService', 'notificationService',
        function ($scope, ngDialog, deConfigService, notificationService) {
            $scope.classObj = {};
            var passBackData = [];
            $scope.cancel = function () {
                passBackData.push({'closeVal': 1});
                console.log(passBackData);
                $parent.$scope.newClassDialog.close(1, passBackData);
            };
            $scope.create = function (isFormValid) {
                if (isFormValid) {
                    $scope.classObj.added_dt = (new Date()).toISOString();
                    $scope.classObj.class_id = 0;
                    deConfigService.createClass($scope.classObj, function (response) {
                        if (response.data) {
                            console.log(response.data);
                            passBackData.push(response.data.data);
                            notificationService.addSuccess('Class created successfully');
                        }
                        else {
                            notificationService.addError('Error!! Please try later');
                        }
                    });
                    $parent.$scope.newClassDialog.close(1, 2);
                }
            };
        }])
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-21
      • 2019-05-13
      • 1970-01-01
      • 1970-01-01
      • 2020-06-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多