【问题标题】:Call function inside $mdDialog from parent controller从父控制器调用 $mdDialog 内的函数
【发布时间】:2019-06-12 22:47:22
【问题描述】:

我需要在 $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() 不是函数

【问题讨论】:

    标签: javascript angularjs angular-material


    【解决方案1】:

    $mgDialog 具有与您的指令不同的隔离范围, 您可以尝试跟踪原始范围并在$mgDialog控制器中使用它

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

    【讨论】:

    • 很高兴它有帮助:)
    猜你喜欢
    • 2017-10-01
    • 2021-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-03
    • 1970-01-01
    • 2014-07-31
    相关资源
    最近更新 更多