34

以下 angular.ui 模态示例显示了modalInstance调用 a ModalIntanceCtrl,它稍后被创建为函数:

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

我有 2 个问题/问题:

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

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

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

  1. 我在这里调用的控制器不是模态实例控制器,而是我的全局控制器loginCtrl,这是一个问题吗?我应该以某种方式子类化 loginCtrl 还是从 ModalInstanceCtrl 调用它?如果是这样 - 究竟如何?

我会很高兴得到有关这方面的指导和澄清。谢谢!

4

4 回答 4

31

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

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

controller: 'ModalInstanceCtrl',

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

于 2013-10-21T06:36:20.687 回答
12

我为像我这样喜欢看例子的人创建了这个plunker 。它展示了如何在不污染全局命名空间的情况下创建模式。希望它是有帮助的。

根据下面bummi的评论,编辑以包含代码示例

索引.html

<html ng-app="app">
  <head>
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.8/angular.js"></script>
    <script src="http://angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.10.0.js">    </script>
<script src="app.js"></script>
    <link href="//netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css"     rel="stylesheet">
  </head>
  <body>

<div ng-controller="appController">
    <script type="text/ng-template" id="myModalContent.html">
    <div class="modal-header">
        <h3>I'm a modal!</h3>
    </div>
    <div class="modal-body">
        <span>Message:{{message}}</span>
        <ul>
            <li ng-repeat="item in items">
                <a ng-click="selected.item = item">{{ item }}</a>
            </li>
        </ul>
        Selected: <b>{{ selected.item }}</b>
    </div>
    <div class="modal-footer">
        <button class="btn btn-primary" ng-click="ok()">OK</button>
        <button class="btn btn-warning" ng-click="cancel()">Cancel</button>
    </div>
</script>

<button class="btn btn-default" ng-click="showModal()">Open me!</button>
<div ng-show="selected">Selection from a modal: {{ selected }}</div>
</div>
  </body>
</html>

应用程序.js

angular.module('app', ['ui.bootstrap']).
service('DataService', ['$rootScope',
  function($rootScope) {
    this.data = {};
    this.data.message = 'This is a message from a service';
    this.data.items = ['item1', 'item2', 'item3'];
  }
]).
controller('myModal', ['$scope', '$modalInstance', 'DataService',
  function($scope, $modalInstance, dataService) {
    $scope.data = dataService.data;
    $scope.message = dataService.data.message;
    $scope.items = dataService.data.items;

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

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

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

  }
]).
controller('appController', ['$scope', '$modal', '$log', 'DataService',
  function($scope, $modal, $log, dataService) {

    $scope.data = dataService.data;

    $scope.showModal = function() {
      var modalInstance = $modal.open({
        templateUrl: 'myModalContent.html',
        controller: 'myModal'
      });

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

    };

  }
]);
于 2014-04-08T15:26:26.133 回答
4

有一个类似的问题并声明了模态控制器而不将其添加到模块中,如下所示:

var ModalInstanceCtrl = ['$scope', '$modalInstance', 'items', function ($scope, $modalInstance, items) { ... }

无需其他更改,此语法也适用于缩小。

于 2014-11-13T16:01:39.350 回答
3

执行此操作的简单方法是使用 $inject:

    // 注入具有以下依赖项的控制器
    ModalInstanceCtrl.$inject = ['$scope', '$modalInstance', 'items'];

将控制器方法更改为命名函数:

    函数 ModalInstanceCtrl($scope, $modalInstance, items) {
        $scope.items = 项目;
        $scope.selected = {
            项目:$scope.items[0]
        };

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

        $scope.cancel = 函数 () {
            $modalInstance.dismiss('取消');
        };
    };

我写了一篇关于这个主题的博客文章,其中包括如何为使用 $inject 的指令编写测试:

过渡到 angular-2-0-part-2

于 2015-06-03T18:26:46.227 回答