javascript - 使用模态从父作用域调用函数( Angular )

标签 javascript angularjs angularjs-scope modal-dialog

我有一个 main.controller.js,在该 Controller 中,我有一个模式,我想用它来通知用户警告,然后再继续应用程序的其余部分。

例如...我想从模式中调用我的 fileNew 函数

main.controller.js:

    $scope.warningModal = function() {
                $modal.open({
                  animation: true,
                  templateUrl: "./modal.html",
                  controller: "modalController",
                });
          };
    $scope.fileNew = function(type, subject, application) {
        newApplication(type, subject, application)
          $state.go('submissions');
        });
      };

main.html:

<li><a ng-click="warningModal()">New app</a></li>

modal.html:

<div class="modal-body">
  <p>warning message</p>
</div>
<div class="modal-footer clearfix">
  <button class="pull-right btn btn-primary" ng-click="$parent.fileNew()">Continue</button>
</div>

模态 Controller :

$scope.$parent.fileNew();

在控制台中给我一条警告消息,说..$scope.$parent.fileNew 不是一个函数。

有什么想法吗?

编辑:

围绕这个想法还有一个简单的问题......

我有 3 种不同类型的文件我想要提交的新应用程序...

所以在我的主controller.js中:

$scope.fileNew = function(type, subject, application) {
        newApplication(type, subject, application)
          $state.go('submissions');
        });
      };
$scope.fileOld = function(type, subject, application) {
        newApplication(type, subject, application)
          $state.go('submissions');
        });
      };
$scope.fileEdit = function(type, subject, application) {
        newApplication(type, subject, application)
          $state.go('submissions');
        });
      };

有什么方法可以让我通过点击模态上相同的继续按钮来传递哪一个吗?

最佳答案

在 Angular UI Bootstrap 的最新版本中,您可以包含一个 scope 选项,您可以在其中传递您希望在模态 Controller 中可用的范围:

$scope.warningModal = function() {
  $modal.open({
     animation: true,
     scope: $scope,
     templateUrl: "./modal.html",
     controller: "modalController",
  });
};

现在您可以从 modalController 中调用您的函数:

$scope.fileNew(type, subject, application);

关于javascript - 使用模态从父作用域调用函数( Angular ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43854382/

相关文章:

.net - 使用 Windows.Forms.Webbrowser.Navigate() 时禁用 Javascript 弹出窗口

javascript - 在javascript中声明静态变量

javascript - 错误 : No controller: ngModel

javascript - AngularJS $scope 变量无缘无故改变值?

javascript - 将第一次出现的 char 替换为 *,然后将其余的替换为 ' '

javascript - 如何准确找出 $scope.$watch 中发生的变化

javascript - 如何从 Angular 表单返回 Django?

javascript - 使用 D3 向静态 SVG 动态添加工具提示

javascript - 变量未定义 : AngularJS

javascript - 遍历对象并返回所有属性数组的值