2

I created a Modal service and when I injected the service into the controller, I am receiving an error that says "$Injector: unpr Unknown Provider". Here is my code below. Let me know if I am missing something.

This is the service I have so far.

'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
        });

    }
};

}]);

Here is the controller I have set up.

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

Here is the template for the data in the modal - 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>

Here is the main home page where I am calling the openBook() to open the modal with the info from the 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 example --inside another array called things

"Books": [
            {
                "name": "Name of Book 1",
                "description": "Description about book..."
            },
            {
                "name": "Name of Book 2",
                "description": "Description about book..."
            }
        ]
2
  • Please provide fiddle or Plunker Commented Oct 14, 2015 at 16:24
  • 2
    Factories dont get a $scope injection. Commented Oct 14, 2015 at 16:25

1 Answer 1

4

This error results from the $injector being unable to resolve a required dependency. To fix this, make sure the dependency is defined and spelled correctly. For example, the following code will fail with the same error you received -$injector:unpr, if myService is not defined:

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

Making sure each dependency is defined will fix the problem, as noted below.

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

So to answer your question, in your case you appear to be missing dependency

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

To Inject modalService like so:

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

    }]);

You also need to change up your factory module to angular.module('myApp.services', ['ui.bootstrap']) and use $uibModal since $modal is deprecated.

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');
    };
}]);

UPDATE 1

As mentioned in the comments, do not attempt to inject $scope to your factory. Here is a Plunker I created which lets you open a modal by calling the factory.

http://plnkr.co/edit/G68NVYZlTqrIS0N2TKL4

Sign up to request clarification or add additional context in comments.

16 Comments

I did this exactly and still it throws an error. I have no idea what it could be.
@LadyT Try my updated answer, make the changes to factory to also inject - $modalInstance.
@LadyT ok, change the module dependency injection to inject in myapp.Services. So do, angular.module('myApp.services', ['ui.bootstrap']) where you factory is. Check my update.
@LadyT Have you downloaded and added path to ui-bootstrap.js in your index.html?
@LadyT Hmm. Do me a favor, include the html if you can so we can see bigger picture of whats going on.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.