This is a rather strange question, but here is the idea:
Say I have a complex JSON object coming back from an HTTP call and attached to $scope . Something like that:
$scope.obj = { user: { id: 10, name: { first: 'Joe', last: 'Smith' }, contact: { home: { street: '101 First St.', city: 'Myville', state: 'Jokelahoma', zip: '98765' }, email: 'joeshmoe@gmail.com', phone: '+12345678901' } }, purchase_hist: [ { item_id: 11004, date: 'Thu, 06 Aug 2015 13:51:17 GMT' }, { item_id: 97020, date: 'Fri, 31 Jul 2015 18:57:57 GMT' } ] }
Now, if I wanted to display an overview of the purchase history in the Angular part, I could do something like this:
<table> <tr ng-repeat="p in obj.purchase_hist"> <td>{{p.item_id}}</td> <td>{{p.date}}</td> </tr> </table>
The really convenient thing in this format (although not too obvious here with so many details) is that the described purchase is called p alias. I do not need to do obj.purchase_hist[0].item_id , I just can do p.item_id .
But what about when I will show the user's home address? Should I really do this ?:
<div> <p>{{obj.user.contact.home.street}}</p> <p>{{obj.user.contact.home.city}}</p> <p>{{obj.user.contact.home.state}}</p> <p>{{obj.user.contact.home.zip}}</p> </div>
This is really verbosity. I would rather use something similar to the controller as ... syntax, something like this:
<div ng-alias="obj.user.contact.home as uhome"> <p>{{uhome.street}}</p> <p>{{uhome.city}}</p> <p>{{uhome.state}}</p> <p>{{uhome.zip}}</p> </div>
Is there such a thing that exists in angular? Unfortunately, I can’t really use plugins in my environment, so I'm specifically looking for the part of the Angular kernel that will work this way.
Thanks!
javascript angularjs alias
Ken bellows
source share