如何将函数传递给Angular UI Bootstrap Modal [英] How to pass a function to angular ui bootstrap modal

查看:95
本文介绍了如何将函数传递给Angular UI Bootstrap Modal的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将函数传递给角度ui引导程序模态对话框的最佳方法是什么?我在模态控制器中创建了一个调用$ scope.$ parent.myMethod()的方法,如下所示:

What is the best way to pass a function to an angular ui bootstrap modal dialog? I created a method in my modal controller that calls $scope.$parent.myMethod() like so:

$scope.isChosen = function(concept) {
    return $scope.$parent.isChosen(concept);
};

这可行,但是我宁愿以类似于将函数传递给指令的方式将函数传递给模态.我尝试使用模式"resolve"参数来执行此操作,但未成功.是否可以为模态解析函数,如果可以,语法是什么?如果不可能的话,除了访问父作用域之外,还有其他方法吗?

This works, but I would rather pass the function to the modal in a similar way to how functions are passed to directives. I've tried to use the modal "resolve" parameter to do this but without success. Is it possible to resolve a function for a modal, and if so, what is the syntax? If not possible, is there any other way to do this other than accessing the parent scope?

这是一个尝试将方法传递给模态的笨拙工具,虽然有点简化,但是却代表了我要执行的操作: http://plnkr.co/edit/eCjbZP

Here is a plunk attempting to pass a method to a modal, it is a little simplified but represents what I'm trying to do: http://plnkr.co/edit/eCjbZP

推荐答案

定义模态时,必须这样解决:

When you are defining your modal , you must resolve like this :

  // here is the controller where you want to trigger the modal to open
 $scope.openModal = function () {
     // somewhere in your html , you may click on a button to envoke openModal()
   var modalInstance = $modal.open({
      templateUrl: 'myModalContent.html',
      controller: ModalInstanceCtrl,
      size: size,
      resolve: {
        isChosen: function () {
          return $scope.isChosen;
        }
     }
   });
};

然后,在您的modalCtr中,您可以像这样注入 isChosen :

And later on , in your modalCtr you can inject isChosen like this :

  app.controller('modalCtrl',function($scope,isChosen){
     // now you can use your isChosen function however you want
  });

这篇关于如何将函数传递给Angular UI Bootstrap Modal的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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