我正在关注一个问题(ui bootstrap modal's controller 'is not defined'),然后我尝试修改嵌套控制器,不同的作用域调用以及重命名/重构代码(用于学习)。
我无法在My Plunker中使用它,所以这给我留下了几个问题:
为什么不起作用?
原始问题显示创建一个新模块,而不是将控制器附加到应用程序模块 - 为什么?这是推荐的吗?
如果PageController
是祖先控制器,它的$scope.open
方法将如何引用其他控制器open
定义?
代码:
var app = angular.module('app', ['ui.bootstrap']);
app.controller('PageController', ['$scope',
function($scope) {
$scope.open = function() {
// how to call ModalController.open?
};
}
])
.controller('ModalController', ['$scope', '$modal', '$log',
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());
});
};
}
])
.controller('ModalInstanceCtrl', ['$scope', '$modalInstance', 'items',
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');
};
}
]);