简体   繁体   English

如何将数据从ngDialog传递回调用方控制器

[英]How to pass data back from ngDialog to caller controller

I'm using AngularJS 1.3 and ngDialog 0.5.9 我正在使用AngularJS 1.3和ngDialog 0.5.9

Opening the dialog using the code similar to this: 使用类似于以下代码的代码打开对话框:

function openDialog() {
    $scope.dataToPassToDialog = myData;
    var dialog = ngDialog.open({
        template: 'template.html',
        className: 'ngdialog-theme-default',
        scope: $scope,
    });

    dialog.closePromise.then(function (data) {

    });
}

I have a small form in the dialog and I need to pass that data back when user closes the dialog. 我在对话框中有一个小表格,当用户关闭对话框时,我需要将该数据传递回去。 Inside dialog controller I'm closing it using : 在对话框控制器内部,我使用以下命令将其关闭:

ngDialog.close();

I need to pass a data object back from dialog to calling controller, I can't find anything in documentation, I tried 我需要将数据对象从对话框传递回调用控制器,我在文档中找不到任何内容,

ngDialog.close(myDataFromDialog);

but I can't access it in any way. 但我无法以任何方式访问它。

How can I do it? 我该怎么做?

Found how to do it, it's pretty easy, from ngDialog controller I can do 从ngDialog控制器中找到了如何做到的,这非常简单

$scope.closeThisDialog(dataToPassBack);

on the page controller 在页面控制器上

function openDialog() {
    $scope.dataToPassToDialog = myData;
    var dialog = ngDialog.open({
        template: 'template.html',
        className: 'ngdialog-theme-default',
        scope: $scope,
    });

    dialog.closePromise.then(function (data) {
        if (data && data.value && /*check if data.value is what you want*/) {
            var dataFromDialog = data.value;
        }
    });
}

create $scope.myDataFromDialog in your controller 在控制器中创建$ scope.myDataFromDialog

and use it in the dialog 并在对话框中使用它

you can find an example in the documentation: 您可以在文档中找到一个示例:

https://github.com/likeastore/ngDialog https://github.com/likeastore/ngDialog

$scope.value = true;

ngDialog.open({
    template: 'externalTemplate.html',
    className: 'ngdialog-theme-plain',
    scope: $scope
});

<script type="text/ng-template" id="externalTemplate.html">
<p>External scope: <code>{{value}}</code></p>
</script>

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

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