In my directive, I am passing a function to a plugin which will use it with a specified value. Let's simplify things by ignoring data changes:
angular.module('some.directives', [])
.directive('myDirective', [,
function () {
return {
restrict: 'EAC',
scope: {
value: '@', // some value
fun: '&' // the function
},
link: function(scope, element, attrs) {
ThePlugin.thisIsTheVal(scope.value);
ThePlugin.thisIsTheFun(scope.fun);
}
};
}
]);
The issue arises when the fun
attribute is not defined or if the parent scope does not have the specified function. In such cases, ThePlugin
defaults to executing angular.noop
, provided by AngularJS.
I need to distinguish between a real undefined
and an undefined function. How can I determine if the calling scope actually contains the required function?
A possible solution could involve checking the function against the target value and returning undefined
only if the result is also undefined
:
ThePlugin.thisIsTheFun(function() {
if (scope.fun(scope.value) === undefined) {
return scope.fun;
}
return undefined;
});
This workaround may introduce inconsistencies as the fun
function might be designed to return undefined
in the future for valid reasons.