如何将对象转换为JSON并将其输出到html页面上?将对象转换为json
public User:UserInfo=
{
firstName: "O",
lastName: "K",
email: "[email protected]",
country: "uk",
avatarUrl: null,
receiveNotifications: true
}
如何将对象转换为JSON并将其输出到html页面上?将对象转换为json
public User:UserInfo=
{
firstName: "O",
lastName: "K",
email: "[email protected]",
country: "uk",
avatarUrl: null,
receiveNotifications: true
}
你可以尝试以下方法:
let jsonStr=JSON.stringify(user).
我可以看到你是从jQuery的未来世界,但angular.js事情变得简单多了,请检查此的jsfiddle:http://jsfiddle.net/pkozlowski_opensource/ASspB/1/
随着angular.js您可以将事件很多,很多简单:
<input type="button" ng-click="showJson()" value="Object To JSON" />
and then in your controller:
$scope.showJson = function() {
$scope.json = angular.toJson($scope.user);
}
其实可以这样做更容易与滤波器angular.js,检查此的jsfiddle:http://jsfiddle.net/pkozlowski_opensource/ASspB/2/其中有:
{{user | json}}
OP是Angular而不是AngularJS – Vega
我希望你给我们你的问题多一点的细节,但我会尽我所能在这里和我一起裸照!比方说你有一个对象,看起来像这样
a.component.ts
public obj = {
name: "Oleg",
question: "convert object to json",
description: "Some cool question about angular and JSON"
}
呈现这个数据,你的看法是这样的
a.component.html
<h1> {{ obj.name }} </h1>
<h2> {{ obj.question }} </h2>
<p> {{ obj.description }} </p>
请注意如何我的班级成员已设置为公开状态,无论何时您要创建申请的AoT版本通货膨胀。
如果您无法理解,请使用我的简化示例。看看这个example
如果你不关心格式{{User | json}}应该可以工作,否则请给出更多的细节,请 – Vega
@oleg哪个版本的angular是这样的? – Abdel
@Abdel Angular2/4 – Oleg