【问题标题】:AngularJS Error Unknown provider: $$jqLiteProvider <- $$jqLite <- $animateCss <- $uibModalStack <- $uibModalAngularJS 错误未知提供程序:$$jqLit​​eProvider <- $$jqLit​​e <- $animateCss <- $uibModalStack <- $uibModal
【发布时间】:2016-04-02 11:53:11
【问题描述】:

我正在尝试创建一个弹出并提供不同菜单选项的简单模式。这应该很容易,我在 ui bootstrap 网站上关注了 Plunker for modals,但出现错误:

$uibModal 是一个未知的提供者

这是角码:

angular.module('billingModule', ['ngAnimate', 'ui.bootstrap']);

angular.module('billingModule').controller('StoreBillingCtrl', function ($scope, $uibModal) {
    $scope.openStoreBilling = function () {
        var modalInstance = $uibModal.open({
            animation: true,
            templateUrl: 'storeBillingContent.html',
            controller: 'ModalInstanceCtrl',
        });
    };
});

angular.module('billingModule').controller('OfficeBillingCtrl', function ($scope, $uibModal) {
    $scope.openOfficeBilling = function () {
        var modalInstance = $uibModal.open({
            animation: true,
            templateUrl: 'officeBillingContent.html',
            controller: 'ModalInstanceCtrl',
        });
    };
});

angular.module('billingModule').controller('ModalInstanceCtrl', function ($scope, $uibModalInstance) {
    $scope.close = function () {
        $uibModalInstance.dismiss();
    }
});

我阅读了错误文档并意识到这是一个依赖错误。但我只是不明白我哪里出错了。我有 angular 1.4.8 和 ui-bootstrap 0.14.3。

这是我添加的脚本:

<head runat="server">
    <title>DP Billing</title>
    <link href="../CSS/bootstrap.css" rel="stylesheet" />
    <link href="../CSS/base.css" rel="stylesheet" />
    <script src="../Scripts/angular.js"></script>
    <script src="../Scripts/angular-animate.js"></script>
    <script src="../Scripts/angular-ui/ui-bootstrap-tpls.js"></script>
    <script src="../Scripts/billing-modals.js"></script>
</head>

【问题讨论】:

  • 请检查您是否添加了script标签。
  • 我编辑了我的问题以包含我为 html 提供的脚本标签。这是你的要求吗?
  • 所以我想通了。事实证明 1.4.8 不适用于 ui-bootstrap。最多只能工作 1.4.7。为什么我不知道。
  • 奇怪,但似乎 1.4.8 可以与 ui-bootstrap 0.14.3 一起使用。您可以更改this plunkr 以重现您的问题吗?
  • 我试图在 plunkr 中复制它,但不能。然后我将我的项目切换回 1.4.8 进行测试,它再次停止工作。然后我决定重新安装 angularJS 以及自项目开始以来我安装的所有包。在此之后它又开始工作了。我唯一的理由是 angular.js 文件在某处被损坏或更改。我很抱歉现在问了这样一个毫无价值的问题。

标签: javascript angularjs angular-ui-bootstrap


【解决方案1】:

您必须使用控制器声明中的括号将依赖项注入控制器。

你有什么:

angular.module('billingModule').controller('StoreBillingCtrl', function ($scope, $uibModal) { ... });

你应该拥有的:

angular.module('billingModule').controller('StoreBillingCtrl', ['$scope', '$uibModal', function ($scope, $uibModal) { ... }]);

这同样适用于其他控制器

更好的风格:

angular.module('billingModule').controller('StoreBillingCtrl', ['$scope', '$uibModal', StoreBillingCtrlFunc]);

StoreBillingCtrlFunc function ($scope, $uibModal) { 
  ... 
}

我建议采用一种风格来避免语法错误。 John Papa's Angular Style Guide 是一个好的开始。

如果您使用该样式,您将清楚地知道您要声明什么以及您要注入什么。更不用说有一个数组,其中除了最后一个之外的所有元素都是依赖项,而最后一个是控制器本身。

angular.module('billingModule').controller('StoreBillingCtrl', StoreBillingCtrlFunc);

StoreBillingCtrlFunc.$inject = ['$scope', '$uibModal'];

StoreBillingCtrlFunc function($scope, $uibModal){
    ...
}

【讨论】:

  • 如果他们缩小代码,OP 会收到完全不同的错误消息。看来它们不是,它们不需要 Angular DI 注释或 .$inject 属性
猜你喜欢
  • 1970-01-01
  • 2017-04-28
  • 1970-01-01
  • 2016-03-30
  • 2016-01-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-09
相关资源
最近更新 更多