1

If I have a string with value 'order.buyer.address.street' and there exists an order object on the scope, how do I get to the address object?

I mean, is there an angular function (such as $parse,...) that can be used to get to the address object?

I know I could split the string and iterate to the object, but I want to know if there is an easier way to do this.

Thanks!

2 Answers 2

1

The easiest solution is to use $scope.$eval:

var address = $scope.$eval('order.buyer.address');
Sign up to request clarification or add additional context in comments.

Comments

1

Well $parse can indeed be used to pull out the address object, as long as the hierarchy is maintained.

var getter = $parse('buyer.address');
var context = order;
var address = getter(context);

2 Comments

I want to parse the full string, as zeroflagL does. I guess I can do var address = $scope.$parse('order.buyer.address')($scope); => What is best to use? the $eval(..) or $parse(...)($scope) ?
Eval can only be used on scope object, If the object is on $scope then $scope.$eval also works

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.