I have a simple directive that passes a function on to a plugin; the plugin will then use the function with a given value. For simplicity, let's ignore changes on the data:
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 problem is: when the fun attribute is not defined or the parent scope doesn't have the specified function, ThePlugin will always execute angular.noop, as that seems to be the default AngularJS provides.
Since I need to pass a real undefined when the function is not defined, how can I know if the calling scope actually has the function?
A workaround may consist in testing the function against the target value, and then return undefined if the resulting value is undefined itself:
ThePlugin.thisIsTheFun(function() {
if (scope.fun(scope.value) === undefined) {
return scope.fun;
}
return undefined;
});
This may lead to some inconsistencies, since it may be that the fun function will be allowed to return undefined as a proper result in the future.
ThePlugin.thisIsTheFun(scope.fun !== angular.noop ? scope.fun : undefined). Maybe it's the same noop function (haven't tested)scope.funis never (directly) equal toangular.noopbut it returns aparentGet(scope, locals)that would later be evaluated to the target function in the caller scope. I have also tried to check$parse(attrs['fun']) === angular.noopbut this only works when the attribute is not set and not, for example, when the attribute is set but the function is not found.scope.fun = (attrs.hasOwnProperty('fun') ? scope.fun : undefined);Maybe a later version of angular has "fixed" this? :) Cause I had this exact issue..