angularjs – 如何将函数传递给角度ui bootstrap模态

前端之家收集整理的这篇文章主要介绍了angularjs – 如何将函数传递给角度ui bootstrap模态前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
函数传递给角度ui bootstrap模态对话框的最佳方法是什么?我在我的模态控制器中创建了一个方法,它调用$scope.$parent.myMethod(),如下所示:

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

这是有效的,但我宁愿将函数传递给模态,其方式与将函数传递给指令的方式类似.我试图使用模态“resolve”参数来做到这一点,但没有成功.是否可以解析模态的函数,如果是,那么语法是什么?如果不可能,除了访问父作用域之外,还有其他方法可以做到这一点吗?

编辑:这是一个尝试将方法传递给模态的插件,它有点简化,但代表我正在尝试做的事:http://plnkr.co/edit/eCjbZP

解决方法

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

// 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:

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

猜你在找的Angularjs相关文章