jQuery 角度模态未关闭($uibModal)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35177305/
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-26 17:01:58  来源:igfitidea点击:

angular modal not closing ($uibModal )

jqueryangularjsmodal-dialogbootstrap-modalui.bootstrap

提问by Scorpio

angular
.module('angProj')
.controller('UserCtrl',
    ['$scope', '$uibModal',
        function ($scope, $uibModal) {

            $scope.results = function (content, completed) {
                var modalInstance = $uibModal.open({
                        backdrop: 'static',
                        keyboard: false,
                        animation: $scope.animationsEnabled,
                        templateUrl: '/Scripts/angularApp/views/user-modal.html',
                        controller: 'UserModalCtrl',
                        resolve: {
                            items: function () {
                                return $scope.items;
                            }
                        }
                    });

                if (!completed || content.length === 0) {
                    return;
                }

        modalInstance.close();
        modalInstance.dismiss('cancel');

I am not able to close the model on user add completion.. On user-modal i am showing progress bar.. the code runs fine without error but the modal remains open. I also tried $uibModalInstance but the controller throws error: unknown provider (not able to inject $uibModalInstance on the same UserCtrl) I am injecting ui.bootstrap (ui-bootstrap-tpls-1.1.2.js)

我无法在用户添加完成时关闭模型.. 在用户模式上我显示进度条.. 代码运行正常而没有错误,但模式保持打开状态。我也试过 $uibModalInstance 但控制器抛出错误:未知提供者(无法在同一个 UserCtrl 上注入 $uibModalInstance)我正在注入 ui.bootstrap (ui-bootstrap-tpls-1.1.2.js)

Thanks for your time.

谢谢你的时间。

回答by Rock

Use modalInstance.close()inside your UserModalCtrlcontroller

modalInstance.close()UserModalCtrl控制器内部使用

app.controller('UserModalCtrl', ['$scope', '$modalInstance' function($scope ,modalInstance {
  $scope.close = function () {
   modalInstance.close();
  };
}]);

回答by sch

I have done something similar, I think.

我也做过类似的事情,我想。

I have a $modalcontroller that looks like this

我有一个$modal看起来像这样的控制器

angular.module('myApp').controller('infoCtrl', function ($scope, $uibModalInstance, loading) {

  $scope.editable = loading;

  $scope.$watch('editable.status', function(newValue, oldValue) {
    if (newValue == 'success'){
        // close modal
        $uibModalInstance.close('ok');
    } else if (newValue == 'error') {
        // show error message
    } 
  });
});

The injected loadingis a service that looks like this

注入的loading是一个看起来像这样的服务

myApp.factory('loading', function() {
  return {
    status: ''
  }
});

And when I change the status of my loading service to 'success' (from anywhere, not the modal controller) the modal is closed.

当我将加载服务的状态更改为“成功”(从任何地方,而不是模态控制器)时,模态将关闭。

I don't know if this is exactly what you were asking for, but I hope it helps, also, just ask if something is unclear!

我不知道这是否正是您所要求的,但我希望它有所帮助,另外,如果有什么不清楚的地方就问吧!

EDIT: So let's say you have a service with a value isCompleted: false, inject that service into your modal controller, and use the $watchfunction, then when that isCompletedis changed to true, you close the modal.

编辑:假设您有一个具有 value 的服务isCompleted: false,将该服务注入到您的模态控制器中,并使用该$watch函数,然后当它isCompleted更改为 时true,您关闭该模态。