javascript - 我如何从 $dialog Controller 获取信息到任何其他 Controller ?

标签 javascript angularjs angular-ui

我正在打开可以随时最小化、带回、编辑并最终提交的对话框。我想做的可能不仅仅是对话,但我希望它能成功。

现在我有了它,这样我就可以调用我的对话框,用现有数据预先填充它,编辑并提交。我现在的主要问题是我不知道如何将最终的、可能经过编辑的数据发送到我客户端中的另一个 Controller 。

$scope.$broadcast 或 $scope.$emit 似乎没有工作。注入(inject)的 Controller 以某种方式驻留在其他 Controller 之外?

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

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

这是我的 Controller :

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

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

最佳答案

总而言之,您调用对话框服务对话框方法,它返回一个 promise ,当该 promise 得到解决时,将调用一个函数,该函数传递数据,该数据传递给对话框 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);
  };
}

关于javascript - 我如何从 $dialog Controller 获取信息到任何其他 Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17552505/

相关文章:

javascript - Noob javascript 1行解释

angularjs - Angular JS 和 Web 套接字

javascript - 单击十字以删除 Angular-UI-Select 的多选框中的选项重定向到应用程序主页

angularjs - Angular ng-model 在编辑时被设置为 undefined

javascript - 在 setTimeOut 中用 addClass 实现的点击动画只发生 oinceWhat

javascript - 如何为用 JS 创建的幻灯片制作动画?

javascript - Facebook 网络数据 API 和 D3.JS

javascript - WordPress 从 Angular JS 功能打开媒体库(ng-click)

javascript - Jasmine:调用一个函数,而不是用 andReturn() 代替

javascript - 设置 AngularUi 应用程序