事实上,我将服务调用转移到了引用控制器,它得到了解决并起了作用。
(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();
}
}
}());