在AngularJs中使用$uibModal时,可以通过在Controller中注入$uibModalInstance来初始化模态框。示例如下:
angular.module('app').controller('myModalCtrl', function ($scope, $uibModalInstance) {
$scope.cancel = function () {
$uibModalInstance.dismiss('cancel');
};
});
其中,$uibModalInstance是一个Service,用于控制模态框的状态。在Controller中,我们可以通过该Service来关闭模态框,如上述的cancel方法。
在调用模态框时,通过controller属性将此Controller与模态框绑定,示例如下:
$uibModal.open({
templateUrl: 'myModalContent.html',
controller: 'myModalCtrl',
size: 'sm',
resolve: {
items: function () {
return $scope.items;
}
}
});