Angular uibModal,Resolve,Unknown Provider

时间:2016-02-11 21:32:22

标签: javascript html angularjs

我试图通过服务公开“通用”模式 - 使用Angular的$uibModal。以下是该服务的定义:

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

上面没什么太复杂的。但是,它不起作用。如果我从对象中删除resolve属性,则服务可以正常工作;但是,如果我包含resolve属性,则会收到源自该属性的Unknown Provider错误。

resolve属性的文档为:

  

(Type:Object) - 将被解析并传递给的成员   控制者作为当地人;它等同于中的resolve属性   路由器。

目标是能够为在其DOM中利用这些属性的模态提供模板,例如:

<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>

我错过了什么导致抛出此错误?

1 个答案:

答案 0 :(得分:31)

你有两个问题:

  1. 您需要在模态配置中定义控制器
  2. 您的解析对象必须是stringfunction的地图,其中string是将被注入模态控制器的依赖关系的名称,以及function是一个工厂函数,用于在实例化控制器时提供依赖性。
  3. 工作示例: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>