Angular UI bootstrap uibModalInstance.result 未产生任何值
Angular UI bootstrap uibModalInstance.result not yielding any value
下面是指令 cabinet
和我的两个控制器 CabinetThumbnails
、ModalCtrl
的代码。根据我的要求,我使用指令 cabinet
和 CabinetThumbnails
来呈现几个小部件,然后使用 ModalCtrl
单击每个小部件打开一个弹出窗口。小部件生成正常,弹出窗口也正常打开,但 (uibModalInstance.result.then(function (thumbnailData))
不工作。所以,它也没有打到服务 cabinetService.getComments(thumbnailData)
。这里有什么问题?想不通。
function () {
'use strict';
angular
.module('myModule')
.directive('cabinet', function () {
return {
restrict: 'E',
replace: true,
controller: CabinetThumbnails,
controllerAs: 'ctrl',
bindToController: true,
link: link,
templateUrl: 'app/cabinet/cabinet.directive.html',
scope: {
thumbnail: '='
}
};
});
function link(scope, el, attrs) {
}
CabinetThumbnails.$inject = ['$scope','$uibModal','cabinetService'];
function CabinetThumbnails($scope,$uibModal,cabinetService) {
var vm = this;
vm.showImage = showImage;
activate();
function activate() {
}
function showImage() {
var uibModalInstance = $uibModal.open({
animation: true,
templateUrl: 'app/components/capture/cabinet.pop-up.html',
controller: ModalCtrl,
controllerAs: 'ctrl',
size: 'lg',
resolve: {
thumbnailData: function () {
return vm.thumbnail;
}
}
});
uibModalInstance.result.then(function (thumbnailData) {
spinner.spinnerShow();
//call the service to get the comments
cabinetService
.getComments(thumbnailData)
.then(function (data) {
$scope.comments = data;
})
.catch(function (err) {
growl.err('Unable to open the screen shot, Please try later !', {ttl: 20000});
})
.finally(spinner.spinnerHide);
}, function () {
$log.info('Modal dismissed at: ' + new Date());
});
}
}
ModalCtrl.$inject = ['$scope', '$uibModalInstance', 'thumbnailData', 'growl'];
function ModalCtrl($scope, $uibModalInstance, thumbnailData, growl) {
var ctrl = this;
ctrl.thumbnailData = thumbnailData;
ctrl.cancel = cancel;
function cancel() {
$uibModalInstance.dismiss();
}
}
}());
这里有几件事需要考虑:
首先你不需要在定义模态时使用controllerAs
,在ui-bootstrap你应该只使用controller: myModalController as vm
(或者你的情况是 ctrl)。
在你的指令中你定义了 controllerAs: 'ctrl',
但你后来使用 vm
.
在模态控制器中,您仅使用 $uibModalInstance.dismiss()
方法,dismiss 方法关闭模态并激活 uibModalInstance.result
promise 的 promise 拒绝处理程序。
您应该使用 $uibModalInstance.close()
来激活解析处理程序。否则所有代码都不会 运行.
我会这样写
spinner.spinnerShow();
$uibModal.open({
animation: true,
templateUrl: 'app/components/capture/cabinet.pop-up.html',
controller: ModalCtrl as ctrl,
size: 'lg',
resolve: {
thumbnailData: function () {
return ctrl.thumbnail;
}
}
}).result
.then(function (thumbnailData) {
//call the service to get the comments
return cabinetService.getComments(thumbnailData);
}, function() {
$log.info('Modal dismissed at: ' + new Date());
}).then(function (data) {
// we get to this 'then' after cabinetService.getComments finished
$scope.comments = data;
}).catch(function (err) {
$log.err('Unable to open the screen shot, Please try later !', {ttl: 20000});
}).finally(spinner.spinnerHide);
}
并添加
ctrl.ok = function() {
$uibModalInstance.close(valueForResolveHandler);
};
到 ModalCtrl
实际上我将服务调用移到了引用控制器,它得到了解决并起作用了。
(function () {
'use strict';
angular
.module('myModule')
.directive('cabinet', function () {
return {
restrict: 'E',
replace: true,
controller: CabinetThumbnails,
controllerAs: 'ctrl',
bindToController: true,
link: link,
templateUrl: 'app/components/capture/cabinet.directive.html',
scope: {
thumbnail: '='
}
};
});
function link(scope, el, attrs) {
}
CabinetThumbnails.$inject = ['$scope','$uibModal'];
function CabinetThumbnails($scope,$uibModal) {
var vm = this;
vm.showImage = showImage;
activate();
function activate() {
}
function showImage() {
var uibModalInstance = $uibModal.open({
animation: true,
templateUrl: 'app/components/capture/cabinet.pop-up.html',
controller: ModalCtrl,
controllerAs: 'ctrl',
size: 'lg',
resolve: {
thumbnailData: function () {
return vm.thumbnail;
}
}
});
}
}
ModalCtrl.$inject = ['$scope', '$uibModalInstance', 'thumbnailData', 'growl','cabinetService'];
function ModalCtrl($scope, $uibModalInstance, thumbnailData, growl,cabinetService) {
var ctrl = this;
ctrl.thumbnailData = thumbnailData;
ctrl.save = save;
ctrl.cancel = cancel;
//call this method to get executed while the directive loads to open the pop-up
getComments();
function getComments()
{
cabinetService
.getComments(thumbnailData)
.then(function (data) {
ctrl.comments = data;
})
.catch(function (err) {
growl.err('Unable to get comments, Please try later !', {ttl: 20000});
})
}
function save() {
var form = $scope.cabinetForm;
if (!form.$invalid && form.$dirty && form.$valid) {
var data = {
CabinetFileID: ctrl.thumbnailData.CabinetFileID,
Comment: ctrl.inputcomment
};
//call the service
cabinetService
.addComments(data)
.then(function (data) {
//refresh the comments by calling the getComments method again
ctrl.thumbnailData.CommentCount = ctrl.thumbnailData.CommentCount + 1;
getComments();
ctrl.inputcomment = '';
})
.catch(function (err) {
growl.err('Unable to add comment, please try later !', {ttl: 20000});
})
} else {
growl.info('Please enter the comment');
}
}
function cancel() {
$uibModalInstance.dismiss();
}
}
}());
下面是指令 cabinet
和我的两个控制器 CabinetThumbnails
、ModalCtrl
的代码。根据我的要求,我使用指令 cabinet
和 CabinetThumbnails
来呈现几个小部件,然后使用 ModalCtrl
单击每个小部件打开一个弹出窗口。小部件生成正常,弹出窗口也正常打开,但 (uibModalInstance.result.then(function (thumbnailData))
不工作。所以,它也没有打到服务 cabinetService.getComments(thumbnailData)
。这里有什么问题?想不通。
function () {
'use strict';
angular
.module('myModule')
.directive('cabinet', function () {
return {
restrict: 'E',
replace: true,
controller: CabinetThumbnails,
controllerAs: 'ctrl',
bindToController: true,
link: link,
templateUrl: 'app/cabinet/cabinet.directive.html',
scope: {
thumbnail: '='
}
};
});
function link(scope, el, attrs) {
}
CabinetThumbnails.$inject = ['$scope','$uibModal','cabinetService'];
function CabinetThumbnails($scope,$uibModal,cabinetService) {
var vm = this;
vm.showImage = showImage;
activate();
function activate() {
}
function showImage() {
var uibModalInstance = $uibModal.open({
animation: true,
templateUrl: 'app/components/capture/cabinet.pop-up.html',
controller: ModalCtrl,
controllerAs: 'ctrl',
size: 'lg',
resolve: {
thumbnailData: function () {
return vm.thumbnail;
}
}
});
uibModalInstance.result.then(function (thumbnailData) {
spinner.spinnerShow();
//call the service to get the comments
cabinetService
.getComments(thumbnailData)
.then(function (data) {
$scope.comments = data;
})
.catch(function (err) {
growl.err('Unable to open the screen shot, Please try later !', {ttl: 20000});
})
.finally(spinner.spinnerHide);
}, function () {
$log.info('Modal dismissed at: ' + new Date());
});
}
}
ModalCtrl.$inject = ['$scope', '$uibModalInstance', 'thumbnailData', 'growl'];
function ModalCtrl($scope, $uibModalInstance, thumbnailData, growl) {
var ctrl = this;
ctrl.thumbnailData = thumbnailData;
ctrl.cancel = cancel;
function cancel() {
$uibModalInstance.dismiss();
}
}
}());
这里有几件事需要考虑:
首先你不需要在定义模态时使用
controllerAs
,在ui-bootstrap你应该只使用controller: myModalController as vm
(或者你的情况是 ctrl)。在你的指令中你定义了
controllerAs: 'ctrl',
但你后来使用vm
.在模态控制器中,您仅使用
$uibModalInstance.dismiss()
方法,dismiss 方法关闭模态并激活uibModalInstance.result
promise 的 promise 拒绝处理程序。 您应该使用$uibModalInstance.close()
来激活解析处理程序。否则所有代码都不会 运行.
我会这样写
spinner.spinnerShow();
$uibModal.open({
animation: true,
templateUrl: 'app/components/capture/cabinet.pop-up.html',
controller: ModalCtrl as ctrl,
size: 'lg',
resolve: {
thumbnailData: function () {
return ctrl.thumbnail;
}
}
}).result
.then(function (thumbnailData) {
//call the service to get the comments
return cabinetService.getComments(thumbnailData);
}, function() {
$log.info('Modal dismissed at: ' + new Date());
}).then(function (data) {
// we get to this 'then' after cabinetService.getComments finished
$scope.comments = data;
}).catch(function (err) {
$log.err('Unable to open the screen shot, Please try later !', {ttl: 20000});
}).finally(spinner.spinnerHide);
}
并添加
ctrl.ok = function() {
$uibModalInstance.close(valueForResolveHandler);
};
到 ModalCtrl
实际上我将服务调用移到了引用控制器,它得到了解决并起作用了。
(function () {
'use strict';
angular
.module('myModule')
.directive('cabinet', function () {
return {
restrict: 'E',
replace: true,
controller: CabinetThumbnails,
controllerAs: 'ctrl',
bindToController: true,
link: link,
templateUrl: 'app/components/capture/cabinet.directive.html',
scope: {
thumbnail: '='
}
};
});
function link(scope, el, attrs) {
}
CabinetThumbnails.$inject = ['$scope','$uibModal'];
function CabinetThumbnails($scope,$uibModal) {
var vm = this;
vm.showImage = showImage;
activate();
function activate() {
}
function showImage() {
var uibModalInstance = $uibModal.open({
animation: true,
templateUrl: 'app/components/capture/cabinet.pop-up.html',
controller: ModalCtrl,
controllerAs: 'ctrl',
size: 'lg',
resolve: {
thumbnailData: function () {
return vm.thumbnail;
}
}
});
}
}
ModalCtrl.$inject = ['$scope', '$uibModalInstance', 'thumbnailData', 'growl','cabinetService'];
function ModalCtrl($scope, $uibModalInstance, thumbnailData, growl,cabinetService) {
var ctrl = this;
ctrl.thumbnailData = thumbnailData;
ctrl.save = save;
ctrl.cancel = cancel;
//call this method to get executed while the directive loads to open the pop-up
getComments();
function getComments()
{
cabinetService
.getComments(thumbnailData)
.then(function (data) {
ctrl.comments = data;
})
.catch(function (err) {
growl.err('Unable to get comments, Please try later !', {ttl: 20000});
})
}
function save() {
var form = $scope.cabinetForm;
if (!form.$invalid && form.$dirty && form.$valid) {
var data = {
CabinetFileID: ctrl.thumbnailData.CabinetFileID,
Comment: ctrl.inputcomment
};
//call the service
cabinetService
.addComments(data)
.then(function (data) {
//refresh the comments by calling the getComments method again
ctrl.thumbnailData.CommentCount = ctrl.thumbnailData.CommentCount + 1;
getComments();
ctrl.inputcomment = '';
})
.catch(function (err) {
growl.err('Unable to add comment, please try later !', {ttl: 20000});
})
} else {
growl.info('Please enter the comment');
}
}
function cancel() {
$uibModalInstance.dismiss();
}
}
}());