如何在$ mdDialog中将关闭数据传递给父级?

时间:2017-04-12 15:26:37

标签: javascript angularjs

我打开Dialog Window(有own controller)。 我想更改属于differentController的{​​{1}}中的数据,Dialog Window将数据发送回onRemoving并应用数据。有可能吗?

parent controller

1 个答案:

答案 0 :(得分:1)

当您在对话框的控制器上调用$mdDialog.hide([response]);函数时,此[response]将作为参数传递给$mdDialog.show()函数返回的承诺。

如果您致电$mdDialog.hide([response]);承诺将得到解决,如果您致电$mdDialog.cancel([response]);承诺将被拒绝。

function openTaskDialog(ev, test)
{
    $mdDialog.show({
        controller         : 'differentController',
        controllerAs       : 'vm',
        templateUrl        : 'dialog.html',
        parent             : angular.element($document.body),
        targetEvent        : ev,
        clickOutsideToClose: true,
        onComplete: onComplete,
        onRemoving: function (event, promise) {
            console.log('onRemoving',event, promise, $mdDialog)
        },
        locals             : {
            Test : test,
            event: ev

        }
    }).then(function(response){

    })
    .catch(function(responseIfRejected){

    })
    .finally(function(){
    })

}

function onComplete (scope, el, options) {
    console.log('scope, el, options', scope, el, options)
}

function differentController($mdDialog){
    this.save = function(){
        $mdDialog.hide({ message: 'success' });
    };
    this.cancel = function(){
        $mdDialog.cancel({ message: 'fail' });
    };
}