/**
*
*
* 初始化:
* var oneModal = modalSvc.createModal(templateUrl, controller, size);
* size可以是:lg或sm
*
* 打开modal并传递数据
* oneModal.show(params);
* 通过参数params可以向modal中传递数据
* <span>{{params.title}}</span>
*
*
* 通过完成事件获得结果
* function onModalComplete(result) {
* }
* oneModal.show(params).then(onModalComplete);
* 其中result 就是modal关闭时的结果,也是hide函数传回的值: hide(result)
* 如果通过其他方式关闭modal,也会调用onModalComplete,但result为undefined
*
*
* 关闭modal
* oneModal.hide(result)
* result作为modal的结果,传回调用者
*
*
* 在模板中关闭modal
* <button ng-click="hide(true)">OK</button>
* <button ng-click="hide()">Cancel</button>
*
*/
angular.module('nCloud.bootstrapModalSvc', ['ui.bootstrap'])
.factory('bootstrapModalSvc', [
'$q', '$rootScope', '$uibModal'
, function ($q, $rootScope, $uibModal) {
function createModal(templateURL, controller, size) { var modalService = {
scope: undefined,
modalWindow: undefined, show: function (params) {
this.scope = $rootScope.$new();
this.scope.params = params;
this.scope.hide = function (result) {
this.$close(result);
}; this.modalWindow = $uibModal.open({
templateUrl: templateURL,
scope: this.scope,
controller: controller,
size: size
}); var q = $q.defer();
this.modalWindow.result.then(
function (result) {
q.resolve(result);
},
function (reason) {
q.resolve();
}
);
return q.promise;
}, hide: function (result) {
if (this.modalWindow) {
this.modalWindow.close(result);
}
}
}; return modalService;
} return {createModal: createModal};
}
])
;
如果未使用uiBootstrap,将$uibModal换为$modal