$ modal.open(...):undefined不是函数

时间:2015-03-18 19:36:40

标签: javascript angularjs twitter-bootstrap

在我的角度控制器中,我想打开一个模态,并检测它何时关闭。然而,文档和我所看到的并没有排成一行。特别是,当调用'$viewContentLoaded'上的处理程序时,我会从$modal服务对象中获得意外结果。

这是我目前的最佳尝试:

controllersModule.controller('MainCtrl', ['$scope','$log','$location', '$modal', 
'$sce', '$q', '$timeout', '$http',
                           function($scope, $log, $location, $modal, $sce, $q, $timeout, $http) {

$scope.$on('$viewContentLoaded', function checkBrowserCompatibility(){
    var m = $modal({title: 'You can read this',
                    content: 'This works',
                    show: true,
                    backdrop: 'static',
                    keyboard: false}); 
}]); 

以上产生了我期望的模态;但是,this建议我应该更多地称它为

var m = $modal.open({title: 'You cannot read this',
                    content: 'This never shows up',
                    show: true,
                    backdrop: 'static',
                    keyboard: false});

它给了我TypeError: undefined is not a function $modal.open(...)我希望能够检测到用户何时关闭模态,但此时它不需要任何更多的行为 - - 没有从模态或类似的东西传回数据。只需弹出一条消息然后继续。

实际上,另一种可接受的(甚至是可取的)行为会阻止模态完全关闭......

2 个答案:

答案 0 :(得分:2)

我建议这个问题可能是由于你的Angular js和Angular Bootstrap UI js库不匹配造成的。

请注意,最新版本的AngularJS Bootstrap UI 0.12.1需要Angular 1.2.16 +

检查您的包含,并且版本兼容。

答案 1 :(得分:0)

你想这样称呼:

   var m = $modal.open({
            content: 'something',
            controller: modalBootstrap,
            backdrop: 'static',
            keyboard: false,
        });

        m.result.then(
            function () {
             //this is called when the modal is closed.
           },
            function () {
             //this is called when the modal is dismissed.
           }
        );

您将使用controller属性将模态的实例传递给模态内的控制器。我们将其称为引导控制器,如下所示:

var modalBootstrap = function ($scope, $modalInstance) {
    $scope.modalInstance = $modalInstance;

};
modalBootstrap['$inject'] = ['$scope', '$modalInstance'];

现在,在模态控制器中,您可以通过调用以下函数之一来关闭模态:

$scope.modalInstance.dismiss();
$scope.modalInstance.close();

其中每一个都将关闭模态并使其对应的回调函数触发。

根据模态是关闭还是解除,m.result中的两个函数将被调用。你可以在那里工作。