未知的提供者:$modalProvider <- AngularJS 的 $modal 错误

2022-01-17 00:00:00 angularjs modal-dialog javascript

我在尝试实现引导模式窗口时不断收到此错误消息.可能是什么原因造成的?我已经复制/粘贴了 http://angular-ui.github.io/bootstrap/#/modal 这里.

I keep receiving this error as I'm trying to implement bootstrap Modal window. What could be the cause of it? I've copy/pasted everything from http://angular-ui.github.io/bootstrap/#/modal here.

推荐答案

当您为控制器、服务等编写依赖项,而您尚未创建或包含该依赖项时,会发生这种错误.

This kind of error occurs when you write in a dependency for a controller, service, etc, and you haven't created or included that dependency.

在这种情况下,$modal 不是已知服务.听起来您在引导角度时没有将 ui-bootstrap 作为依赖项传递.angular.module('myModule', ['ui.bootstrap']); 另外,为了安全起见,请确保您使用的是最新版本的 ui-bootstrap (0.6.0).

In this case, $modal isn't a known service. It sounds like you didn't pass in ui-bootstrap as a dependency when bootstrapping angular. angular.module('myModule', ['ui.bootstrap']); Also, be sure you are using the latest version of ui-bootstrap (0.6.0), just to be safe.

该错误在 0.5.0 版本中引发,但更新到 0.6.0 确实使 $modal 服务可用.因此,请更新到 0.6.0 版本,并确保在注册模块时需要 ui.boostrap.

The error is thrown in version 0.5.0, but updating to 0.6.0 does make the $modal service available. So, update to version 0.6.0 and be sure to require ui.boostrap when registering your module.

回复您的评论:这就是您注入模块依赖项的方式.

Replying to your comment: This is how you inject a module dependency.

<!-- tell Angular what module we are bootstrapping -->
<html ng-app="myApp" ng-controller="myCtrl">

js:

// create the module, pass in modules it depends on
var app = angular.module('myApp', ['ui.bootstrap']);

// $modal service is now available via the ui.bootstrap module we passed in to our module
app.controller('myCtrl', function($scope, $uibModal) {

});

更新:

$modal 服务已重命名为 $uibModal.

使用 $uibModal 的示例

// create the module, pass in modules it depends on
var app = angular.module('myApp', ['ui.bootstrap']);

// $modal service is now available via the ui.bootstrap module we passed in to our module
app.controller('myCtrl', function($scope, $uibModal) {
    //code here
});

相关文章