向控制器添加服务时的未知提供程序

时间:2015-10-14 16:21:02

标签: javascript html angularjs angular-ui-bootstrap angular-services

我创建了一个Modal服务,当我将服务注入控制器时,我收到一条错误消息“$ Injector:unpr Unknown Provider”。这是我的代码如下。如果我错过了什么,请告诉我。

这是我到目前为止的服务。

'use strict';

angular.module('myApp.services', [])

.factory('modalService', ['$scope', function($scope) {
return {
    openMenuModal: function(templateLink, windowAnimation) {
        var modalInstance = $modal.open({
            templateUrl: templateLink,
            backdrop: 'static',
            windowClass: windowAnimation,
            controller: function($scope, $modalInstance) {
                $scope.close = function() {
                    $modalInstance.close();
                };
            },
            size: 'md',
            scope: $scope,
            keyboard: true
        });

    }
};

}]);

这是我设置的控制器。

angular.module('myApp.controllers', ['ui.bootstrap', 'ngAnimate'])
.controller('HomeCtrl', function($scope, $http, $modal, modalService) {
    $scope.opentheBook = modalService.openMenuModal('partials/Books.html', 'animated zoomIn');
});

以下是模式中数据的模板 - Books.html

<div ng-controller="HomeCtrl">
<div class="modalBox animated fadeIn">
<button class="btn btn-danger pull-right" type="button" ng-click="" tooltip="Close"><i class="fa fa-times"></i></button>
<h1>title</h1>
<p>description</p>
<div class="next">
<button class="btn btn-danger pull-right" type="button" tooltip="Close"><i class="fa fa-times"></i></button>
            </div>
        </div>
    </div>

这是我打电话给openBook()的主页,用json中的信息打开模态

    <div class="Books">
                  <ul>
                    <li ng-repeat="book in thing.Books" class="list-unstyled"><a ng-click="opentheBook" href="#"><h6>{{book.name}}</h6></a></li>
                   </ul>
            </div>

json for Books示例 - 另一个名为thing的数组

"Books": [
            {
                "name": "Name of Book 1",
                "description": "Description about book..."
            },
            {
                "name": "Name of Book 2",
                "description": "Description about book..."
            }
        ]

1 个答案:

答案 0 :(得分:4)

$injector无法解析所需的依赖关系导致此错误。要解决此问题,确保依赖关系已定义并拼写正确。例如,如果未定义myService,则以下代码将失败并收到相同的错误 - $injector:unpr

angular.module('myApp', [])
.controller('MyController', ['myService', function (myService) {
  // Do something with myService
}]);

确保定义每个依赖项将解决问题,如下所示。

angular.module('myApp', [])
.service('myService', function () { /* ... */ })
.controller('MyController', ['myService', function (myService) {
  // Do something with myService
}]);

所以要回答你的问题,在你的情况下,你似乎缺少依赖

angular.module('myApp.controllers', ['ui.bootstrap', 'ngAnimate'])
.controller('HomeCtrl', function($scope, $http, $modal, modalService) {
    $scope.opentheBook = modalService.openMenuModal('partials/Books.html', 'animated zoomIn');
});

像这样注入modalService

.controller('HomeCtrl', ['modalService', function($scope, $http, $modal, modalService) {

    }]);

您还需要将factory模块更改为angular.module('myApp.services', ['ui.bootstrap'])并使用$uibModal,因为$modal已被弃用。

angular.module('myApp', ['ui.bootstrap'])

.factory('modalService', ['$uibModal', function($uibModal) {

  return {
    openMenuModal: function(templateLink, windowAnimation) {

        var modalObj = $uibModal.open({
            templateUrl: templateLink,
            backdrop: 'static',
            windowClass: windowAnimation,
            controller: function($scope,$modalInstance){
              $scope.ok = function(id){
                //Process OK Button Click
                 $modalInstance.close(); 
              },
               $scope.cancel = function(){
                $modalInstance.dismiss('cancel');
              }
            },
            size: 'md',
            keyboard: true,
            resolve: {
              someData: function () {
                return 'Return some Data';
              }
          }
        });
    }
};
}])

.controller('HomeCtrl', ['$scope','modalService', function($scope, modalService, someData) {
   $scope.dataFromService = someData;
   $scope.opentheBook = function(){
      modalService.openMenuModal('myModalContent.html', 'animated zoomIn');
    };
}]);

更新1

如评论中所述,请勿尝试向您的工厂注入$scope。这是我创建的一个Plunker,它允许您通过调用工厂打开模态。

http://plnkr.co/edit/G68NVYZlTqrIS0N2TKL4