0

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?

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot to you both!
1

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

2 Comments

You are welcome. Care to share a working fiddle so we can both learn more?
Well Hawk's answer is only good if you want to hard code things. Its important to understand what is happening under the hood here. The Angular notation can be expanded to standard notation. You can do it by hand like hawk suggests, but you are better off doing it dynamically with the proper tools as I mentioned.

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.