我是 angularjs 的新手,正在尝试创建一个单页应用程序。我有一个代码非常简单的家庭控制器。类似于本教程的内容
Angular JS 控制器使用如下代码初始化:
var phonecatApp = angular.module('phonecatApp', []);
phonecatApp.controller('PhoneListCtrl', function ($scope) {
$scope.phones = [
{'name': 'Nexus S',
'_id': 1,
'snippet': 'Fast just got faster with Nexus S.'},
{'name': 'Motorola XOOM™ with Wi-Fi',
'_id': 2,
'snippet': 'The Next, Next Generation tablet.'},
{'name': 'MOTOROLA XOOM™',
'_id': 3,
'snippet': 'The Next, Next Generation tablet.'}
];
});
但在生产数据中可能没有那么整齐地打包。现在我的问题是:
我可以创建指向我的对象的 JSON 表示的下载链接吗?
<li ng-repeat="phone in phones">
<a
href="data:text/json;charset=utf-8,{{encodeURIComponent(JSON.stringify(phone))}}"
download="{{phone._id}}.json">
JSON
</a>
</li>
我基本上想phone
用格式化功能访问当前对象encodeURIComponent(JSON.stringify(phone))
。
有没有办法巧妙地做到这一点?