简体   繁体   English

如何在angularjs中将数据从对话框传回控制器?

[英]how to pass back the data from dialog to controller in angularjs?

below is my first controller下面是我的第一个控制器

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

    }])

In my second controller am trying to send some data to my parent controller as shown below在我的第二个控制器中,我试图向我的父控制器发送一些数据,如下所示

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

below is the ngdialog html.下面是 ngdialog html。 It has 2 textbox which am able to get data to my second controller but not able to send response back to first controller它有 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>

Am pushing some objects to the array and trying to send but not able to receive it from parent controller.正在将一些对象推送到数组并尝试发送但无法从父控制器接收它。

Where am doing wrong?哪里做错了?

This might work (can't test it unless you can share a plunker):这可能有效(除非您可以共享一个plunker,否则无法对其进行测试):

.controller('configManagementCtrl', ['$scope', 'deConfigService', 'ngDialog', '$state', 'notificationService', function ($scope, deConfigService, ngDialog, $state, notificationService) { .controller('configManagementCtrl', ['$scope', 'deConfigService', 'ngDialog', '$state', 'notificationService', function ($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();
            }
        });
    };

}])

and in the other controller:在另一个控制器中:

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

After a closer read of the documentation, it looks like you need to call .close() passing the id of the dialog and the value to return from the dialog's controller.仔细阅读文档后,您似乎需要调用.close()传递对话框的 id 和从对话框控制器返回的值。 In your parent controller the object passed back to your closePromise callback has id and value properties.在您的父控制器中,传递回closePromise回调的对象具有idvalue属性。 You'll need to get whatever you're passing back via the value property (ie data.value.whateverYouAreReturning ).您需要获取通过value属性传回的任何内容(即data.value.whateverYouAreReturning )。 Here is a simple example that returns an object with a single string property.这是一个简单的示例,它返回一个具有单个字符串属性的对象。

 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>

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

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