Javascript 如何从其他控制器关闭 Angular-ui 模式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27852861/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 00:47:07  来源:igfitidea点击:

How to close Angular-ui modal from other controller

javascriptangularjsangular-uiangular-ui-bootstrap

提问by Cocorico

I am using Angular-ui to pop up a modal with a form in it. My code is:

我正在使用 Angular-ui 弹出一个带有表单的模式。我的代码是:

app.controller('NewCaseModalCtrl', ['$http', '$scope','$modal', function ($http, $scope, $modal, $log) {

  $scope.items = ['item1', 'item2', 'item3'];
  $scope.open = function (size) {

    var modalInstance = $modal.open({
      templateUrl: 'modal-new-case.html',
      controller: 'ModalInstanceCtrl',
      size: size,
      resolve: {
        items: function () {
          return $scope.items;
        }
      }
    });

    modalInstance.result.then(function (selectedItem) {
      $scope.selected = selectedItem;
    }, function () {
    });
  };
}]);

And then I have another controller that is inside the modal-new-case.html template, and I want it to run an httpd request and then close that modal, here is that code:

然后我在 modal-new-case.html 模板中有另一个控制器,我希望它运行一个 httpd 请求,然后关闭该模态,这是代码:

    app.controller('CreateCaseFormCtrl', ['$http','$scope', function($http,$scope) {
    $scope.formData = {};
    $scope.processForm = function() {

        $http.post('http://api.com/proj', $scope.formData).
        success(function(data, status, headers, config) {
            console.log("success " + data.id);
        }).
        error(function(data, status, headers, config) {
            console.log("Error " + status + data);
        });
    };

}]);

}]);

Okay so inside my modal-new-case.html template, which is loaded when I do:

好的,在我的 modal-new-case.html 模板中,当我这样做时会加载它:

ng-controller="NewCaseModalCtrl"

I have this HTML:

我有这个 HTML:

<div ng-controller="CreateCaseFormCtrl">
    <form ng-submit="processForm()">
                <button class="btn btn-primary" ng-click="processForm()" >OK</button>
                <button class="btn" ng-click="cancel()">Cancel</button>
    </form>
</div>

So if you see, what I really want to do is to run that processForm() function, and when it returns with a success, I want to THEN call the function that will close the modal, which I believe "cancel()" would be fine.

所以如果你看到了,我真正想做的是运行那个 processForm() 函数,当它成功返回时,我想调用关闭模态的函数,我相信“cancel()”会没事的。

But I don't know how to refer to it from the CreateCaseFormCtrl controller.

但我不知道如何从 CreateCaseFormCtrl 控制器中引用它。

I appreciate any thoughts and help, and I would like to add that I am very unsophisticated when it comes to Angular, so if this is complicated, please remember that maybe I am not 100% clear on what every single thing in Angular is such as the factories and such. I guess I'm saying I'm very happy with a dirty solution that's fairly simple, since this isn't going to be long-term production programming code.

我感谢任何想法和帮助,我想补充一点,我对 Angular 非常不熟练,所以如果这很复杂,请记住,也许我不是 100% 清楚 Angular 中的每一件事是什么,例如工厂之类的。我想我是说我对一个相当简单的肮脏解决方案感到非常满意,因为这不会是长期的生产编程代码。

采纳答案by Yang Li

Step 1:remove the

第 1 步:删除

ng-controller="CreateCaseFormCtrl"

from

<div ng-controller="CreateCaseFormCtrl">
    <form ng-submit="processForm()">
                <button class="btn btn-primary" ng-click="processForm()" >OK</button>
                <button class="btn" ng-click="cancel()">Cancel</button>
    </form>
</div>

Step 2:Change

第 2 步:更改

controller: 'ModalInstanceCtrl',   =>   controller: 'CreateCaseFormCtrl'

in

var modalInstance = $modal.open({
  templateUrl: 'modal-new-case.html',
  controller: 'CreateCaseFormCtrl', //Add here
  size: size,
  resolve: {
    items: function () {
      return $scope.items;
    }
  }
});

Step 3:In CreateCaseFormCtrl add a service called $modalInstance

第 3 步:在 CreateCaseFormCtrl 添加一个名为 $modalInstance 的服务

app.controller('CreateCaseFormCtrl', ['$http','$scope', '$modalInstance', function($http,$scope, $modalInstance) {

Step 4:Add the close and ok functions

第四步:添加close和ok函数

$scope.cancel = function () {
    $modalInstance.dismiss();
};

and $modalInstance.close(); in

和 $modalInstance.close(); 在

$http.post('http://api.com/proj', $scope.formData).
    success(function(data, status, headers, config) {
        console.log("success " + data.id);
        $modalInstance.close(); //add here
    }).
    error(function(data, status, headers, config) {
        console.log("Error " + status + data);
    });

回答by huan feng

use $modalInstance.dismiss API

使用 $modalInstance.dismiss API

in NewCaseModalCtrl:

在 NewCaseModalCtrl 中:

controller('NewCaseModalCtrl', ['$scope', '$modalInstance', function ($scope, $modalInstance,

    ...

        $modalInstance.close(data);

回答by hakuna

You an do it globally like this or from other controllers too:

您可以像这样全局执行此操作,也可以从其他控制器执行此操作:

 //hide any open $mdDialog modals
  angular.element('.modal-dialog').hide();
  //hide any open bootstrap modals
  angular.element('.inmodal').hide();
  //hide any sweet alert modals
  angular.element('.sweet-alert').hide();