AngularJS injection Error - Unknown provider: modalMessagesProvider <- modalMessages

Viewed 161

I have a service opening an angular-ui-bootstrap modal with a component controller. But my controller can't get access to the parameter i am passing on (modalMessages, i just want to print it). The error is: Error: [$injector:unpr] Unknown provider: modalMessagesProvider <- modalMessages

Can anyone help?

Service:

angular.module('app').service('AlertService', function ($uibModal) {
    this.showModal = function (modalMessages) {
        return $uibModal.open({
            component: "modalComponent",
            resolve: {
                modalMessages: function () {
                    return modalMessages;
                }
            }
        }).result;
    }
});

Component Controller:

'use strict';

const Modal = {
    templateUrl: 'views/modals/modalAlert.html',
    controller: ['modalMessages', ModalCtrl],
    controllerAs: '$ctrl',
    bindings: {
        modalMessages: "<",
    }
}


angular.module('app').component('modalComponent', Modal);

function ModalCtrl() {

    this.modalMessages = modalMessages;
    console.log(this.modalMessages);
}
1 Answers

Take a look at this plunkr

Notice the changes I have done in:

(function(){
  const Modal = {
    templateUrl: 'modalAlert.html',
    controller:  ModalCtrl,
    bindings: {
      resolve: "<"
    }
};

angular.module('app').component('modalComponent', Modal);

function ModalCtrl() {
  var $ctrl = this;
  $ctrl.$onInit = function() {
    $ctrl.modalMessages = $ctrl.resolve.modalMessages;
  }
}

})()

you need to use resolve with bindings to access the passed paramaeter

Related