10

I'm creating a large amount of directives and all will include dynamic scope variables that will be initialised inside the link functions e.g:

//
link: function(scope, ele, attr){
  scope.key = scope.somevar + 'something_else';
  scope[scope.key] = 'the_value';
}
//

I want to access the value in the templates of the directives viascope.key.

<div ng-if="scope[key]"> something </div>

Currently I only see it been viable through a function call like so:

html

<div ng-if="scope(key)"> something </div>

js

scope.scope = function(key) {
  return scope[key];
}

But then the problem is I will need to copy this into all the directives.

Another option I considered was to assign the getter function onto the $rootScope making it globally accessible but how do I bind it to or pass in the current directives scope. (If even possible).

What would be a good approach to this?

1
  • scope[key] doesn't work because you don't have a scope object on the scope. Try ng-if="keyContainer[key]" and having scope.keyContainer = {}; scope.keyContainer[scope.key] = *val* Commented Dec 22, 2015 at 13:32

3 Answers 3

17

Inside of Angular template this keyword points to the current evaluation context, i.e. current scope. It means that you would be able to achieve what you are after by using bracket notation on the this object:

<div ng-if="this[key]"> something </div>
Sign up to request clarification or add additional context in comments.

3 Comments

I hang my head in shame for not trying this. Pardon the pun. Thank you!
Using this it's very dangerous, because some directives creates a own scope. The better way it's use the 'controllerAs' syntax.
@GonzaloPincheiraArancibia I would agree with you that controllerAs is generally better choice for many things. However, the point of the question was how to access dynamic scope property.
2

Use bindToController option in your directive

JS

bindToController: true,
controller: 'MyController',
controllerAs: 'ctrl',
link: function(scope, ele, attr){
  scope.ctrl.key = scope.somevar + 'something_else';
  scope.ctrl[scope.ctrl.key] = 'the_value';
}

HTML

<div ng-if="ctrl[ctrl.key]"> something </div>

Check this codepen as example: https://codepen.io/gpincheiraa/pen/LGZrde

2 Comments

Thanks for the insight, I wasn't aware you could do this.
ControllerAs is indeed would be good idea. However, not the way you use it in your example. You don't want to have controller alias in link function. bindToController is the way to go in this case.
0

It would be easier to see all your code, but it sounds like you could just create a function on your scope to retrieve the value:

scope.getValue = function() {
    return scope[scope.key];
}

Then in your HTML:

<div ng-if="getValue()"> something </div>

2 Comments

Thank you for the answer, however you have just duplicated what I've provided in the question just replacing the names.
sorry.. that scope.scope was a little confusing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.