mvermand
mvermand

Reputation: 6137

Angular parse string to scope object

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!

Upvotes: 1

Views: 1597

Answers (2)

a better oliver
a better oliver

Reputation: 26828

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

var address = $scope.$eval('order.buyer.address');

Upvotes: 1

Chandermani
Chandermani

Reputation: 42669

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);

Upvotes: 1

Related Questions