在Angular中的另一个控制器中使用控制器

时间:2013-10-03 08:18:04

标签: angularjs angular-ui angular-ui-bootstrap

modal in angular-ui示例在ModalInstanceCtrl控制器中使用ModalDemoCtrl实现,如下所示:

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

    var modalInstance = $modal.open({
      ...
      controller: ModalInstanceCtrl,
     ...
    });
    ...
  };
};

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

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

尝试使用角度控制器时尝试这种方法:

app.controller('ModalInstanceCtrl', ['$scope', '$modal', '$log',
 function ($scope, $modalInstance, items) {
  ...

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

app.controller('ModalDemoCtrl', ['$scope', '$modal', '$log', 'ModalInstanceCtrl',
 function ($scope, $modal, $log, ModalInstanceCtrl) {
  $scope.open = function () {

    var modalInstance = $modal.open({
      ...
      controller: ModalInstanceCtrl,
     ...
    });
    ...
  };
 }]);

我收到以下错误:

Error: Unknown provider: ModalInstanceCtrlProvider <- ModalInstanceCtrl

是否可以以角度嵌入这样的控制器?

1 个答案:

答案 0 :(得分:4)

您无需将ModelInstanceCtrl注入ModalDemoCtrl。控制器定义应为

app.controller('ModalDemoCtrl', ['$scope', '$modal', '$log',
 function ($scope, $modal, $log) {

没有这个也应该有效。如果没有尝试

 var modalInstance = $modal.open({
      ...
      controller: 'ModalInstanceCtrl',
     ...
    });