这是一个奇怪的问题,但我的想法是:
假设我有一个复杂的 JSON 对象,它从 HTTP 调用返回并附加到$scope
。像这样的事情:
$scope.obj = {
user: {
id: 10,
name: { first: 'Joe', last: 'Smith' },
contact: {
home: {
street: '101 First St.',
city: 'Myville',
state: 'Jokelahoma',
zip: '98765'
},
email: '[email protected] /cdn-cgi/l/email-protection',
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' }
]
}
现在,如果我想在 AngularJS 部分中显示购买历史记录的概述,我可以这样做:
<table>
<tr ng-repeat="p in obj.purchase_hist">
<td>{{p.item_id}}</td>
<td>{{p.date}}</td>
</tr>
</table>
这种格式真正方便的一点(尽管这里道具很少,所以不是很明显)是所描述的购买别名为p
。我不必做obj.purchase_hist[0].item_id
,我可以这样做p.item_id
.
但是当我去显示用户的家庭住址时呢?我真的必须这样做吗?:
<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>
这实在是太啰嗦了。我更愿意使用类似的东西controller as ...
语法,类似这样:
<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>
AngularJS 中存在这样的东西吗?不幸的是,我不太能够在我的环境中使用插件,所以我专门寻找以这种方式工作的 Angular Core 的一部分。
Thanks!