I'm building a custom validator as a directive for a component, and I am trying to pass an attribute, but I am not being able to retrieve the value, just the string of the method I am calling.
Part of the code is:
directive:
return {
restrict: 'A',
require: 'ngModel',
link: (scope, element, attributes, ngModel) => {
const myAttrib = attributes.myValidation ;
console.log(myAttrib);
...
}
component:
my-validation="$ctrl.getmyAtt()"
controller:
getmyAtt() { return "blah"; }
so in the console I get "$ctrl.getmyAtt()" instead of blah. How do I get blah?
You'll need to inject the $parse
service and use it to get the actual value being passed in.
$parse(attributes.myValidation)($scope);
$parse
documentation here.
edit: Actually, that won't work for your callback parameter. Instead you'll want to store that string and run it as javascript at a later time when the callback needs to be run.
var returnValue = scope.$eval(myAttrib);