简体   繁体   English

Angular uibModal,Resolve,Unknown Provider

[英]Angular uibModal, Resolve, Unknown Provider

I am trying to expose a "generic" modal - using Angular's $uibModal - through a service. 我试图通过服务公开一个“通用”模式 - 使用Angular的$ uibModal 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; 如果我从对象中删除resolve属性,该服务工作; however, if I include the resolve property, I get the Unknown Provider error originating from that property. 但是,如果我包含resolve属性,则会收到源自该属性的未知提供程序错误。

The documentation for the resolve property reads: resolve属性的文档如下:

(Type: Object) - Members that will be resolved and passed to the controller as locals; (类型:对象) - 将被解析并作为本地传递给控制器​​的成员; it is equivalent of the resolve property in the router. 它相当于路由器中的resolve属性。

The objective is to be able to provide a template for the modal that utilizes these properties in its DOM, eg : 目标是能够为在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>

What am I missing that is causing this error to be thrown? 我错过了什么导致抛出此错误?

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. 您的resolve对象需要是stringfunction的映射,其中string是将被注入模态控制器的依赖项的名称,而function是一个工厂函数,用于在实例化控制器时提供该依赖项。

Working example: JSFiddle 工作示例: JSFiddle

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 uibModal提供程序未知unitTest - uibModal provider unknown unitTest 未知提供程序:3Provider &lt;-3使用Angular Bootstrap uibModal - Unknown provider: 3Provider <- 3 using Angular Bootstrap uibModal 带有异步角化的角度$ uibmodal解析 - Angular $uibmodal resolve with asynchornization 未知提供程序:$ uibModalProvider &lt;-$ uibModal &lt;-modalPopDirective - Unknown provider: $uibModalProvider <- $uibModal <- modalPopDirective 角UIRouter解析未知的提供程序 - Angular UIRouter resolve unknown provider Angular Bootstrap uibModal无法解析属性 - Angular Bootstrap uibModal not resolve attributs 未知提供者:$ animateCssProvider &lt;-$ animateCss &lt;-$ uibModalStack &lt;-$ uibModal - Unknown provider: $animateCssProvider <- $animateCss <- $uibModalStack <- $uibModal angular-dashboard-framework给我错误未知提供程序:$ uibModalProvider &lt;-$ uibModal &lt;-adfDashboardDirective - angular-dashboard-framework give me error Unknown provider: $uibModalProvider <- $uibModal <- adfDashboardDirective AngularJS错误未知提供程序:$$ jqLit​​eProvider &lt;-$$ jqLit​​e &lt;-$ animateCss &lt;-$ uibModalStack &lt;-$ uibModal - AngularJS Error Unknown provider: $$jqLiteProvider <- $$jqLite <- $animateCss <- $uibModalStack <- $uibModal Angular - 来自提供商的未知提供商 - Angular - Unknown Provider from Provider
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM