When creating numerous directives with dynamic scope variables initialized in the link functions, accessing these values within the directive templates can get tricky. For example:
//
link: function(scope, ele, attr){
scope.key = scope.somevar + 'something_else';
scope[scope.key] = 'the_value';
}
//
The desired access via scope.key
in the directive templates proves challenging without a workaround. One option is to create a function call like:
html
<div ng-if="scope(key)"> something </div>
js
scope.scope = function(key) {
return scope[key];
}
This method requires repetitive copying into all the directives, which may not be the most efficient solution. Alternatively, considering assigning the getter function to $rootScope
for global access raises questions on binding it to or passing it into the current directive's scope.
What would be the optimal approach to resolve this issue?