以下 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 个问题/问题:
文档建议以另一种方式创建控制器(由于缩小问题),例如:
myApp.controller('GreetingCtrl', ['$scope', function($scope) { $scope.greeting = 'Hola!'; }]);
但是如果我这样创建控制器,我怎么能把它注入到modalInstance中呢?
- 我在这里调用的控制器不是模态实例控制器,而是我的全局控制器
loginCtrl
,这是一个问题吗?我应该以某种方式子类化 loginCtrl 还是从 ModalInstanceCtrl 调用它?如果是这样 - 究竟如何?
我会很高兴得到有关这方面的指导和澄清。谢谢!