How to pass data back from ngDialog to caller controller

3.3k views Asked by At

I'm using AngularJS 1.3 and 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?

2

There are 2 answers

1
devadrion On

create $scope.myDataFromDialog in your controller

and use it in the dialog

you can find an example in the documentation:

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>
0
Burjua On

Found how to do it, it's pretty easy, from ngDialog controller I can do

$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;
        }
    });
}