2015-02-05 57 views
0

我试图有一个简单的模式出现时,用户点击我的演示应用程序仍在建设中的东西。试图解雇一个简单的角模态导致:无法读取属性'解雇'的未定义

一切工作到我希望用户点击模式上的'关闭'按钮。当他们这样做,他们得到:

TypeError: Cannot read property 'dismiss' of undefined

这是我在我的主控制器:

$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 is made available for injection in the controller by AngularUI Bootstrap implementation. So, we don't need any effort ro "resolve" or make it available somehow.

+1

Stewie正确回答。你应该摆脱这种表示法,并且在缩小脚本时使用* ngmin *或* ngannotate *。 – Ioan 2015-02-05 14:38:43

回答

2

您没有将您的$modalInstance参数声明为undefined,因为它没有将您的内联注释数组声明为依赖项,并声明ModalInstanceCtrl控制器。

它应该是:

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

的“我们不需要任何努力...”的一部分,并不意味着你不必指定它作为一个依赖。

+0

啊,好的。谢谢。这更像我现在所期待的。 – 2015-02-05 14:44:11

3

我能够把事情简单化:在我的模式模板

$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> 

按照文档:

In addition the scope associated with modal's content is augmented with 2 methods:

$close(result)

$dismiss(reason)

Those methods make it easy to close a modal window without a need to create a > dedicated controller.

我的确尝试过这个伯爵更呃,但我猜想要么是其他的干扰,要么我没有清除我的缓存。