在 angular.ui 模态中注入角度控制器依赖项的正确方法 [英] The correct way to inject an angular controller dependency inside an angular.ui modal

查看:18
本文介绍了在 angular.ui 模态中注入角度控制器依赖项的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下 angular.ui Modal example 显示了 modalInstance 调用一个 ModalIntanceCtrl,它稍后被创建为一个函数:

following angular.ui Modal example shows the modalInstance calling a ModalIntanceCtrl which is later created as a function:

var ModalDemoCtrl = function ($scope, $modal, $log) {

  $scope.items = ['item1', 'item2', 'item3'];

  $scope.open = function () {

    var modalInstance = $modal.open({
      templateUrl: 'myModalContent.html',
      controller: ModalInstanceCtrl,
      resolve: {
        items: function () {
          return $scope.items;
        }
      }
    });

    modalInstance.result.then(function (selectedItem) {
      $scope.selected = selectedItem;
    }, function () {
      $log.info('Modal dismissed at: ' + new Date());
    });
  };
};

var ModalInstanceCtrl = function ($scope, $modalInstance, items) {

  $scope.items = items;
  $scope.selected = {
    item: $scope.items[0]
  };

  $scope.ok = function () {
    $modalInstance.close($scope.selected.item);
  };

  $scope.cancel = function () {
    $modalInstance.dismiss('cancel');
  };
};

我有两个问题:

  1. 文档建议以另一种方式创建控制器(由于缩小问题),例如:

  1. the docs recommend creating a controller in another way (due to minification issues) for example:

myApp.controller('GreetingCtrl', ['$scope', function($scope) {$scope.greeting = '你好!';}]);

但是如果我像这样创建控制器,我怎么能将它注入到 modalInstance 中?

But if I create the controller like this, how could I Inject it into the modalInstance?

  1. 我在这里调用的控制器不是模态实例控制器而是我的全局loginCtrl,这是一个问题吗?我应该以某种方式继承 loginCtrl 还是从 ModalInstanceCtrl 调用它?如果是这样 - 究竟如何?
  1. The controller I call here isn't a Modal Instance controller but my global loginCtrl, is this a problem? should I subclass somehow the loginCtrl or call it from the ModalInstanceCtrl? and if so - how exactly?

我很高兴得到有关此方面的指导和说明.谢谢!

I'll be glad for guidance and clarfication about this. Thanks!

推荐答案

你的问题不是很清楚,但是如果你使用模块 API 声明控制器,那么你可以将控制器作为字符串提供给模态服务

You question is not very clear, but if you declare controller using the module API, then you can provide the controller to the modal service as a string

myApp.controller('ModalInstanceCtrl', ['$scope', function($scope) { $scope.greeting = 'Hola!'; }]);

controller: 'ModalInstanceCtrl',

如果您想在模态服务中使用 loginCtrl,也可以这样做.

The same can be done for loginCtrl if you want to use that in the modal service.

这篇关于在 angular.ui 模态中注入角度控制器依赖项的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆