简体   繁体   English

从父控制器调用 $mdDialog 内的函数

[英]Call function inside $mdDialog from parent controller

I need to call a function in $mdDialog .我需要在$mdDialog 中调用一个函数 This function is being passed to my directive from the parent.这个函数正从父级传递给我的指令。

<get-list callback="getList()" ></get-list>

To get the function on my get-list directive.在我的 get-list 指令中获取函数。

function directive() {

  return {
    restrict: 'E',
    scope: {
      callback: '&?'
    },
    templateUrl: "",
    controller: function($scope) {
      'ngInject';

}

now inside my get-list directive, I have a $mdDialog.现在在我的 get-list 指令中,我有一个 $mdDialog。

  $scope.save = function(){
    $mdDialog.show({
      templateUrl: '',
      escapeToClose: true,
      clickOutsideToClose: true,

      controller: function($scope) {

        $scope.teste = function(){
          $scope.callback()
        }

      }
    })
  }

I need to call the function getList() inside it, and I'm getting the error $scope.callback() is not a function我需要在其中调用函数 getList(),但收到错误$scope.callback() is not a function

$mgDialog has an isolated scope that is different from the one of your directive, you can try to keep track of the original scope and use it in $mgDialog controller $mgDialog有一个与您的指令不同的隔离范围,您可以尝试跟踪原始范围并在$mgDialog控制器中使用它

 $scope.save = function(){
    var outerScope = $scope;
    $mdDialog.show({
      templateUrl: '',
      escapeToClose: true,
      clickOutsideToClose: true,
      controller: function($scope) {
        $scope.teste = function(){
          outerScope.callback();
        }
      }
    })
  }

or pass the callback as a parameter或将回调作为参数传递

$scope.save = function(){
    $mdDialog.show({
      templateUrl: '',
      escapeToClose: true,
      clickOutsideToClose: true,
      locals: {
        callback: $scope.callback
      },
      controller: function($scope, callback) {
        $scope.teste = function(){
          callback();
        }
      }
    })
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM