1

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

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

4

1 回答 1

4

你不需要注入ModelInstanceCtrl你的ModalDemoCtrl. 控制器定义应该是

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

没有这个它也应该工作。如果不尝试

 var modalInstance = $modal.open({
      ...
      controller: 'ModalInstanceCtrl',
     ...
    });
于 2013-10-03T08:59:31.130 回答