尝试消除简单的角度模态会导致:无法读取未定义的属性“消除”

Mik*_*use 1 twitter-bootstrap angularjs angular-ui

当用户单击我的演示应用程序中仍在构建的内容时,我试图显示一个简单的模式。

一切正常,直到我希望用户单击模式上的“关闭”按钮。当他们这样做时,他们会得到:

类型错误:无法读取未定义的属性“解除”

这是我的主控制器中的内容:

    $scope.underConstruction = function () {
    var modalInstance = $modal.open({
        templateUrl: 'app/shared/underconstruction.html',
        controller: 'ModalInstanceCtrl',
        size: 'sm',
        resolve: {
            '$modalInstance': function () { return function () { return modalInstance; } }
        }
    });
    console.log('modal opened');
    modalInstance.result.then(function (response) {
        $scope.selected = response;
        console.log(response);
    }, function () {
        console.log('Modal dismissed at: ' + new Date());
    });
};
Run Code Online (Sandbox Code Playgroud)

然后在我的 ModalInstanceCtrl 中我有:

app.controller('ModalInstanceCtrl', ['$scope', '$modal', function ($scope, $modal, $modalInstance) {
$scope.cancel = function () {
    $modalInstance.dismiss('cancel');
};
}]);
Run Code Online (Sandbox Code Playgroud)

我正在使用 Angular-UI 版本 0.12.0,AngularJS 版本 v1.3.11

close()方法被命中,然后抛出上述错误。

我查看了各种结果和问题,并看到了对错误和其他问题的引用,但用例比我的更复杂 - 我的模式只显示一些文本和一个关闭按钮。例如,我发现一个答案说

$modalInstance 可通过 AngularUI Bootstrap 实现注入到控制器中。因此,我们不需要任何努力来“解决”或以某种方式使其可用。

Mik*_*use 6

我能够简化事情:

$scope.underConstruction = function () {
    var modalInstance = $modal.open({
        templateUrl: 'app/shared/underconstruction.html'
    });
    console.log('modal opened');
};
Run Code Online (Sandbox Code Playgroud)

然后在我的模态模板中:

  <button class="btn btn-primary" ng-click="$dismiss('cancel')">Close this message</button>
Run Code Online (Sandbox Code Playgroud)

根据文档:

此外,与模态内容相关的范围还通过 2 种方法进行了扩展:

$关闭(结果)

$解雇(原因)

这些方法可以轻松关闭模式窗口,而无需创建专用控制器。

我之前确实尝试过此操作,但我想要么是其他原因干扰了,要么是我没有清除缓存。