EricC
EricC

Reputation: 5870

AngularJS: Why doesn't the directive see an object on the scope

I am experimenting with Angular. I have altered this fiddle for the purpose of this question.

When I try to access the scope variables "password" and "password_confirmation" from the directive, like this it displays them fine. See this fiddle. Like this:

scope.$apply(function(){
          scope.passwordAsSeenByDirective = scope['password'] + "...";
          scope.password_confirmationAsSeenByDirective = scope['password_confirmation'] + "...";
        });

But when I make an object and put these as properties there, then I cannot access them any more. See this fiddle. Like this:

scope.$apply(function(){
          scope.passwordAsSeenByDirective = scope['obj.password'] + "...";
          scope.password_confirmationAsSeenByDirective = scope['obj.password_confirmation'] + "...";
        });

How come?

Upvotes: 0

Views: 40

Answers (2)

hawk
hawk

Reputation: 5408

try this scope['obj']['password']

Upvotes: 1

jwvanderbeck
jwvanderbeck

Reputation: 67

I can't give you the complete answer, as I am still learning AngularJS myself. However one bit of help I can impart is that you can't access a variable like scope["foo.bar"]. "foo.bar" is an Angular Expression, not a property syntax and therefore must be either parsed with $parse or evaluated with scope.$eval

Upvotes: 1

Related Questions