javascript - 从父 Controller 调用 $mdDialog 内的函数

标签 javascript angularjs angular-material

我需要在 $mdDialog 中调用一个函数。此函数正在从父级传递到我的指令。

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

在我的 get-list 指令中获取函数。

function directive() {

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

}

现在在我的 get-list 指令中,我有一个 $mdDialog。

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

      controller: function($scope) {

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

      }
    })
  }

我需要在其中调用函数 getList(),但出现错误 $scope.callback() 不是函数

最佳答案

$mgDialog 有一个与您的指令不同的隔离范围, 您可以尝试跟踪原始范围并在 $mgDialog Controller

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

或者将回调作为参数传递

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

关于javascript - 从父 Controller 调用 $mdDialog 内的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54257889/

相关文章:

javascript - Angular 6 路由器过渡动画不起作用(导航栏)

javascript - 如何设置页面中所有链接的标题

javascript - 将过滤器应用于除第一行之外的所有行

javascript - jQuery - 对每个()元素重复每个()但在循环之间重置

javascript - AngularJS $http get 请求未正确编码对象内的日期

angular - mat-form-field 总是显示所需的红色?

html - 将一个 div 置于 2 个 div 之上的 css 指南

javascript - Promises数组,传递创建时使用的变量

javascript - Bootstrap下拉菜单点击后消失

javascript - AngularJS - 基于 API 的实时搜索不会随着 ng-model 的变化而更新