Javascript Angular uibModal、Resolve、Unknown Provider

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35350463/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 17:37:07  来源:igfitidea点击:

Angular uibModal, Resolve, Unknown Provider

javascripthtmlangularjs

提问by Thomas

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

我正在尝试通过服务公开一个“通用”模式——使用 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
    };
}]);

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

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

The documentation for the resolveproperty 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.

(类型:对象)- 将作为本地人解析并传递给控制器​​的成员;它相当于路由器中的解析属性。

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

目标是能够为在其 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?

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

回答by Shaun Scovil

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 stringis the name of the dependency that will be injected into your modal's controller, and functionis a factory function that will be used to provide that dependency when the controller is instantiated.
  1. 您需要在模态配置中定义控制器
  2. 你的决心对象需要是地图的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>