AngularJS 注入错误 - 未知提供者:modalMessagesProvider <- modalMessages

AngularJS injection Error - Unknown provider: modalMessagesProvider <- modalMessages

我有一个服务打开一个带有组件控制器的 angular-ui-bootstrap 模态。 但是我的控制器无法访问我传递的参数(modalMessages,我只想打印它)。 错误是:错误:[$injector:unpr] 未知提供者:modalMessagesProvider <- modalMessages

有人可以帮忙吗?

服务:

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

组件控制器:

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

看看这个plunkr

请注意我所做的更改:

(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;
  }
}

})()

您需要使用 resolvebindings 来访问传递的参数