Javascript 引导模式未关闭

Javascript 引导模式未关闭,javascript,angularjs,bootstrap-modal,angular-ui-bootstrap,Javascript,Angularjs,Bootstrap Modal,Angular Ui Bootstrap,我正在使用带有angular js的UI引导模式对话框。模态已成功加载。但当我单击“是/否”按钮时,发出的消息发生了&模态没有关闭 错误为“$uibModal.close不是函数” 这是我的服务 .service('ConfirmService', function($uibModal) { var service = {}; service.open = function (text, onOk) { var modalInstance = $uibModal.

我正在使用带有angular js的UI引导模式对话框。模态已成功加载。但当我单击“是/否”按钮时,发出的消息发生了&模态没有关闭

错误为“$uibModal.close不是函数”

这是我的服务

.service('ConfirmService', function($uibModal) {
    var service = {};
    service.open = function (text, onOk) {
        var modalInstance = $uibModal.open({
            templateUrl: 'modules/confirmation-box/confirmation-box.html',
            controller: 'userListCtrl',
            resolve: {
                text: function () {
                    return text;
                }
            }
        });

        modalInstance.result.then(function (selectedItem) {
            onOk();
        }, function () {
        });
    };

    return service;
})
这是我的控制器文件。我试图在控制器内按下是/否按钮

.controller('userListCtrl',
  ['$scope','$http','appConfig','$uibModalInstance', '$uibModal','$log','alertService',
  function ($scope,$http, appConfig,$uibModalInstance, $uibModal,$log,alertService) {

    $scope.ok = function () {
        $uibModalInstance.close();
    };

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

}]);

您试图同时使用两种使用方法。有两个(可能更多)可以使用$uibModal,但我认为这两个是混合使用的:

1) 服务控制模式并返回承诺,我相信这就是我认为您正在做的。在这种情况下,您不需要手动调用close/Disclose。您可以进行以下更改:

service.open = function(text, onOK) {
    var modalInstance = $uibModal.open({
        templateUrl: 'modules/confirmation-box/confirmation-box.html',
        controller: 'userListCtrl',
        resolve: {
            text: function () {
                return text;
            }
        }
    });

    // Return so you can chain .then just in case.  Generally we don't even 
    // do this, we just return the instance itself and allow the controller to
    // decide how to handle results/rejections
    return modalInstance.result;
}
在模板文件中,您将有如下内容:

<button type="button" ng-click="$close(selectedItem)"></button>
<button type="button" ng-click="$dismiss(readon)"></button>
然后在控制器中:

var modal = service.open('confirm');
modal.result.then(...)
modal.close()

编辑-根据GeorgeWG的建议,通过对op的更改进行更新以删除反模式

$uibModal
服务只有一种方法:
open(options)
。因此,错误消息是正确的。第一次编辑将
$uibModal.close()
更改为
$uibModalInstance.close()
。这是故意的,因为它确实改变了整个问题。是一个反模式。
ng click
属性是一个核心AngularJS指令。将该属性名称用于表达式绑定会造成混淆,因为它与核心ng click指令的作用不同。请包含“是”和“否”按钮的HTML。当问题不包括有轨电车按钮时,很难回答有关有轨电车按钮的问题。*是反模式。同意。因此,注释指出应该返回结果,而不是在服务中调用onOk。我不想改变太多的作品。
...
return $uibModal.open({});
var modal = service.open('confirm');
modal.result.then(...)
modal.close()