如何从 $dialog 控制器获取信息到任何其他控制器? [英] How do I get information from a $dialog controller to any other controller?

查看:22
本文介绍了如何从 $dialog 控制器获取信息到任何其他控制器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在打开可随时最小化、恢复、编辑并最终提交的对话框.我想要做的可能不仅仅是一个对话,但我希望它能解决问题.

I am opening Dialogs that can be minimized, brought-back, edited at any time and finally submitted. What I'm trying to do might be more than just a dialog but I'm hoping that it can work out.

现在我有了它,这样我就可以召唤我的对话框,用现有数据预先填充它,编辑和提交.我现在的主要问题是我不知道如何将最终的、可能已编辑的数据发送到我客户端的另一个控制器.

Right now I have it so that I can summon my dialog, pre-populate it with existing data, edit and submit. My major problem right now is that I have no idea how to send the final, possibly edited data to another controller in my client.

$scope.$broadcast 或 $scope.$emit 似乎不起作用.. 注入的控制器以某种方式驻留在其他控制器之外?

$scope.$broadcast or $scope.$emit doesn't appear to be working.. The injected controller resides outside of the others somehow?

这是我创建对话框的方式:

This is how I'm creating my dialog:

$scope.openDialog = function(index){
    var html = $scope.buildHTML(index);

    var opts = {
        resizeable: true,
        backdrop: false,
        handle: ".modal-header",
        template: html,
        controller: 'OpenItemCtrl',
        resolve: {
            itemModel: function() {              
                return $scope.item[index];
            }
        }
    };

    var d = $dialog.dialog(opts);
    d.open().then(function() {
        // Right here I can determine that a dialog has closed.
        alert(index);
    });
};

这是我的控制器:

function OpenItemCtrl($scope, dialog, itemModel) {
    $scope.item= {};

    for(key in itemModel) {
        $scope.item[key] = itemModel[key];
    }

    $scope.close = function(qty, src, price){
        // I need to get these edited variables back 
        // into my controller classes somehow...
        $scope.$emit("ItemFinalized", {msg:$scope.item});
        $scope.$broadcast("ItemFinalized", {msg:$scope.item});
        dialog.close();
    };
}

如何将最终数据返回到我的控制器层次结构中,以便我可以根据需要传递它们?

How can I get my final data back into my controller hierarchy so I can pass them around as I need to?

推荐答案

总而言之,您调用对话服务对话框方法,它返回一个承诺,当该承诺得到解决时,将调用一个函数,该函数传递的数据是传递给对话框控制器中的对话框关闭调用.

Long and short of it, you call the dialog services dialog method it returns a promise, when that promise is resolved a function is called that is passed the data that is passed to the dialog close call in the dialog's controller.

http://angular-ui.github.io/bootstrap/#/dialog

$scope.opts = {
    backdrop: true,
    keyboard: true,
    backdropClick: true,
    template:  t, // OR: templateUrl: 'path/to/view.html',
    controller: 'TestDialogController'
  };

$scope.openDialog = function(){
    var d = $dialog.dialog($scope.opts);
    d.open().then(function(result){
      if(result)
      {
        alert('dialog closed with result: ' + result);
      }
    });
  };


// the dialog is injected in the specified controller
function TestDialogController($scope, dialog){
  $scope.close = function(result){
    dialog.close(result);
  };
}

这篇关于如何从 $dialog 控制器获取信息到任何其他控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆