试图解雇一个简单的角度模态导致:无法读取属性' dismiss'未定义的

时间:2015-02-05 13:43:51

标签: angularjs twitter-bootstrap angular-ui

当用户点击我的演示应用程序中仍在构建的内容时,我正在尝试使用简单的模式。

一切都达到了我希望用户点击模态上的“关闭”按钮的程度。当他们这样做时,他们得到:

  

TypeError:无法读取未定义

的属性'dismiss'

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

    $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());
    });
};

然后在我的ModalInstanceCtrl中我有:

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

我正在使用angular-ui版本0.12.0,angularjs版本v1.3.11

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

我已经查看了各种结果和问题,并看到了对错误和其他问题的引用,但是用例比我的更复杂 - 我的模态只显示了一些文本和一个关闭按钮。例如,我found an answer that says

  

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

3 个答案:

答案 0 :(得分:4)

我能够简化事情:

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

然后在我的模态模板中:

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

根据文件:

  

此外,与模态内容相关的范围增加了两种方法:

     

$接近(结果)

     

$驳回(原因)

     

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

我之前尝试过这个,但我想其他的东西都在干扰,或者我没有清除我的缓存。

答案 1 :(得分:2)

你故意&#34;定义&#34;您的$modalInstance参数为undefined,并未将其声明为内联注释数组中的依赖关系,您将在其中声明ModalInstanceCtrl控制器。

应该是:

['$scope', '$modal', '$modalInstance', function($scope, $modal, $modalInstance){ ... }]

&#34; 我们不需要任何努力...... &#34;部分并不意味着您不必将其指定为依赖项。

答案 2 :(得分:0)

对我有用

if(typeof(alert/popover)!="undefined"){
    await alert/popover.dismiss();
  }