最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Angular uibModal, Resolve, Unknown Provider - Stack Overflow

programmeradmin18浏览0评论

I am trying to expose a "generic" modal - using Angular's $uibModal - through a service. Here is the definition of that service:

angular.module('app').service('CustomModalService', ['$uibModal', function ($uibModal) {

    var openCustomModal = function (size, title, message) {
        var actionToPerformOnConfirm = action;

        var modalInstance = $uibModal.open({
            templateUrl : 'templates/CustomModal.html',
            size: size,
            resolve: {
                title: title,
                message: message
            }
        });
    };

    return {
        openCustomModal: openCustomModal
    };
}]);

Nothing too complicated, above. However, it is not working. If I remove the resolve property from the object, the service works; however, if I include the resolve property, I get the Unknown Provider error originating from that property.

The documentation for the resolve property reads:

(Type: Object) - Members that will be resolved and passed to the controller as locals; it is equivalent of the resolve property in the router.

The objective is to be able to provide a template for the modal that utilizes these properties in its DOM, e.g. :

<div ng-controller="CustomModalController">
    <div class="modal-header">
        <h3 class="modal-title">{{title}}</h3>
    </div>
    <div class="modal-body">
        {{message}}
    </div>
    <div class="modal-footer">
        <button class="ad-button ad-blue" type="button" ng-click="confirmAction()"></button>
        <button class="ad-button ad-blue" type="button" ng-click="cancelAction()"></button>
    </div>
</div>

What am I missing that is causing this error to be thrown?

I am trying to expose a "generic" modal - using Angular's $uibModal - through a service. Here is the definition of that service:

angular.module('app').service('CustomModalService', ['$uibModal', function ($uibModal) {

    var openCustomModal = function (size, title, message) {
        var actionToPerformOnConfirm = action;

        var modalInstance = $uibModal.open({
            templateUrl : 'templates/CustomModal.html',
            size: size,
            resolve: {
                title: title,
                message: message
            }
        });
    };

    return {
        openCustomModal: openCustomModal
    };
}]);

Nothing too complicated, above. However, it is not working. If I remove the resolve property from the object, the service works; however, if I include the resolve property, I get the Unknown Provider error originating from that property.

The documentation for the resolve property reads:

(Type: Object) - Members that will be resolved and passed to the controller as locals; it is equivalent of the resolve property in the router.

The objective is to be able to provide a template for the modal that utilizes these properties in its DOM, e.g. :

<div ng-controller="CustomModalController">
    <div class="modal-header">
        <h3 class="modal-title">{{title}}</h3>
    </div>
    <div class="modal-body">
        {{message}}
    </div>
    <div class="modal-footer">
        <button class="ad-button ad-blue" type="button" ng-click="confirmAction()"></button>
        <button class="ad-button ad-blue" type="button" ng-click="cancelAction()"></button>
    </div>
</div>

What am I missing that is causing this error to be thrown?

Share Improve this question asked Feb 11, 2016 at 21:32 ThomasThomas 6,6706 gold badges43 silver badges71 bronze badges 9
  • 2 1) Do you have ui-bootstrap installed and injected into your index.html? 2) Make sure to inject ui.bootstrap as well into your service. 3) Make sure you have latest ui.bootstrap installed. – Stephan K. Commented Feb 11, 2016 at 21:38
  • I have ui-bootstrap installed and injected, yes. – Thomas Commented Feb 11, 2016 at 21:40
  • Ok.. because in the service you have shown it is not injected. This might help as well - github.com/angular-ui/bootstrap/issues/4603 – Stephan K. Commented Feb 11, 2016 at 21:41
  • @StephanKristyn does it need to be injected into the service, itself, for it to work? It is working, just to restate again, without the resolve property. – Thomas Commented Feb 11, 2016 at 21:43
  • I'm pretty sure you need a controller when you use resolve. – Matthew Green Commented Feb 11, 2016 at 22:02
 |  Show 4 more comments

1 Answer 1

Reset to default 32

You have two problems:

  1. You need to define the controller in your modal config
  2. Your resolve object needs to be a map of string: function, where string is the name of the dependency that will be injected into your modal's controller, and function is a factory function that will be used to provide that dependency when the controller is instantiated.

Working example: JSFiddle

JavaScript

angular.module('myApp', ['ui.bootstrap'])
  .controller('MyModalController', MyModalController)
  .directive('modalTrigger', modalTriggerDirective)
  .factory('$myModal', myModalFactory)
;

function MyModalController($uibModalInstance, items) {
  var vm = this;
  vm.content = items;
  vm.confirm = $uibModalInstance.close;
  vm.cancel = $uibModalInstance.dismiss;
};

function modalTriggerDirective($myModal) {
  function postLink(scope, iElement, iAttrs) {
    function onClick() {
      var size = scope.$eval(iAttrs.size) || 'lg'; // default to large size
      var title = scope.$eval(iAttrs.title) || 'Default Title';
      var message = scope.$eval(iAttrs.message) || 'Default Message';
      $myModal.open(size, title, message);
    }
    iElement.on('click', onClick);
    scope.$on('$destroy', function() {
      iElement.off('click', onClick);
    });
  }

  return {
    link: postLink
  };
}

function myModalFactory($uibModal) {
  var open = function (size, title, message) {
    return $uibModal.open({
      controller: 'MyModalController',
      controllerAs: 'vm',
      templateUrl : 'templates/CustomModal.html',
      size: size,
      resolve: {
        items: function() {
          return {
            title: title,
            message: message
          };
        }
      }
    });
  };

  return {
    open: open
  };
}

HTML

<script type="text/ng-template" id="templates/CustomModal.html">
  <div class="modal-header">
    <h3 class="modal-title">{{vm.content.title}}</h3>
  </div>
  <div class="modal-body">
    {{vm.content.message}}
  </div>
  <div class="modal-footer">
    <button class="ad-button ad-blue" type="button" ng-click="vm.confirm()">
      confirm
    </button>
    <button class="ad-button ad-blue" type="button" ng-click="vm.cancel()">
      cancel
    </button>
  </div>
</script>

<button modal-trigger size="'sm'" title="'Hello World!'" message="'This is a test'">
  Click Me
</button>
发布评论

评论列表(0)

  1. 暂无评论