Basically i have a factory
angular.module('app').factory('gService',gService);
function gService($filter, $window) {
function confirmDialog(message, success, fail) {
var confirmMessage = navigator.notification.confirm(
message,
onConfirm,
'',
[$filter('translate')('OK'), $filter('translate')('CANCEL')]
);
function onConfirm(index) {
return index === 1 ? success() : fail();
}
return confirmMessage;
}
}
I want to check condition outside this factory, if the functions are executed or not
if(gService.confirmDialog.onConfirm){
}
this does not work. How do i check function which is executed in angular?
If you check onConfirm
event that it will controll is onConfirm
function defined on gService.confirmDialog
object where the statement wroten. It is not async and promised job.
if(gService.confirmDialog.onConfirm){
}
You need to notify your listeners first. After that listen that event to do your job.
You can broadcast
or emit
an event to scopes that waiting for onConfirm
event.
angular.module('app').factory('gService',gService);
function gService($rootScope, $filter, $window) {
function confirmDialog(message, success, fail) {
var confirmMessage = navigator.notification.confirm(
message,
onConfirm,
'',
[$filter('translate')('OK'), $filter('translate')('CANCEL')]
);
function onConfirm(index) {
var result = index === 1 ? success() : fail();
$rootScope.$emit('onConfirm', result);
//or
//$rootScope.$broadcast('onConfirm', result); -> this goes downwards to all child scopes. Emit is upwarded.
}
return confirmMessage;
}
}
After that you should check if onConfirm
event is triggered. This does the controll you need.
function onConfirmFunction( result ){ //You will get the success or fail methods result here... };
$rootScope.$on('onConfirm', onConfirmFunction);
Why don't you do this.
angular.module('app').factory('gService',gService);
function gService($filter, $window) {
var obj = {
confirmDialog : confirmDialog,
onConfirm : onConfirm,
}
obj.executed = false;
function confirmDialog(message, success, fail) {
var confirmMessage = navigator.notification.confirm(
message,
onConfirm,
'',
[$filter('translate')('OK'), $filter('translate')('CANCEL')]
);
function onConfirm(index) {
if(index===1){
//I dont know you want function or returned value of function but you can use success() as well
obj.executed = success;
return success();
}else{
obj.executed = fail;
return fail();
}
}
return confirmMessage;
}
return obj;
}
you can check now..
if(gService.executed){
}
and I dont know if you forgot but you haven't returned anything from factory.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With