0

I'm trying to pass an object into a modal dialog in a bootstrap/angularJS using the code below. I did this in the style of the answer given at AngularJS UI Bootstrap modal is unable to perform functions from scope. When the modal form is supposed to open from a call to editGroup(), I get the following error:

Error: [$injector:unpr] Unknown provider: selGroupProvider <- selGroup

var EditGroupModalController = function ($scope, $modalInstance, selGroup) {        
        $scope.user = $sessionStorage.user;
        $scope.selGroup = selGroup;

        $scope.closeModal = function () {
            $modalInstance.close();
        };
    };

    $scope.editGroup = function (selGroup) { // "selGroup" receives the current Group object from $scope.groupList[]
        $modal.open({
            templateUrl: 'app/views/administration/advanced/editgroup.html',
            controller:  ['$scope', '$modalInstance','$modal','$sessionStorage','advancedService','selGroup', EditGroupModalController],
            size: 'lg',
            windowTemplateUrl:'app/views/partials/modaltemplatedraggable.html',
            backdrop:'static',
            resolve: {
                item: function () {
                    return selGroup;
                }
            }
        });
    };

The official description of this error is here; however, I do not understand why I am receiving this error. Any help with this would be greatly appreciated.

2 Answers 2

2
  1. Your controller's dependencies list doesn't match controller's function definition: in $modal.open you've listed six dependencies, while in function only three are present.
  2. Dependencies are injected by resolve keys - in your case the key is item.

Necessary changes to your code in order to make it work:

Replace (1)

var EditGroupModalController = function ($scope, $modalInstance, selGroup)

with

var EditGroupModalController = function ($scope, $modalInstance, $modal, $sessionStorage, advancedService, selGroup)

And replace (2)

resolve: {
    item: function () {

With

resolve: {
    selGroup: function () {
Sign up to request clarification or add additional context in comments.

1 Comment

Your corrections worked perfectly. Thank you for the explanation, as well!
0

You should name the variable in the resolve as 'selGroup' instead of 'item' so that it can be correctly injected.

Also, the names of the declared dependencies should match the function definition. I put together this simple demo.

angular.module('test', ['ui.bootstrap']).controller('TestController', function($scope, $modal) {
  var sel = {name: "A group"};
  var EditGroupModalController = function($scope, $modalInstance, selGroup) {
    $scope.selGroup = selGroup;
    $scope.closeModal = function() {
      $modalInstance.close();
    };
  };

  $scope.editGroup = function(selGroup) { // "selGroup" receives the current Group object from $scope.groupList[]
    $modal.open({
      template: '<div>Test {{selGroup.name}}</div>',
      controller: ['$scope', '$modalInstance', 'selGroup', EditGroupModalController],
      size: 'lg',
      backdrop: 'static',
      resolve: {
        selGroup: function() {
          return sel;
        }
      }
    });
  };
});

http://plnkr.co/edit/ec1PjkDZtf4yqINONnX5?p=preview

Comments

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.