这是我的角度代码
$scope.StartCPU = function () {
//Initialize the Timer to run every 1000 milliseconds i.e. one second.
$scope.GetCPUData = $interval(function(){
$http.get('http://localhost:3034/api/test/metrics').
success(function(data, status, headers, config) {
$scope.Details = data;
});}, 1000);
};
Html代码如下
<div ng-controller="HttpGetController">
<button ng-click="StartCPU()">Get All Data</button>
<h1>CPU data</h1>
<pre>{{Details | json}}</pre>
</div>
我收到的回复如下
{
"data": [
{
"_id": "57ee5641931b429431085fc4",
"system": 0.06249,
"process": 0.0091652,
"timestamp": "2016-09-30T12:10:40.780Z",
"__v": 0,
"createdAt": "2016-09-30T12:10:41.927Z"
}
]
}
现在我想访问键“system”和“process”的值。我该怎么办?
答案 0 :(得分:2)
data.data[0].system
和data.data[0].process
说明:您的data
是Object
,其中data
属性。
//complete thing is your response data
{
"data": [ // data.data <--array
{ // data.data[0] <--first object of the array
"_id": "57ee5641931b429431085fc4",
"system": 0.06249,// data.data[0].system <--its property
"process": 0.0091652,
"timestamp": "2016-09-30T12:10:40.780Z",
"__v": 0,
"createdAt": "2016-09-30T12:10:41.927Z"
}
]
}
答案 1 :(得分:1)
在您从通话中获得的data
对象中,您拥有属性data
这是一个数组
然后你应该调用数组的第一个元素
data.data[0]
哪会让你回来
{
"_id": "57ee5641931b429431085fc4",
"system": 0.06249,
"process": 0.0091652,
"timestamp": "2016-09-30T12:10:40.780Z",
"__v": 0,
"createdAt": "2016-09-30T12:10:41.927Z"
}
然后你调用你需要的元素
data.data[0].system
data.data[0].process
答案 2 :(得分:0)
这意味着它返回一个数组,你可以使用这样的东西:
<table class="table datatable">
<thead>
<tr>
<th>system</th>
<th>process</th>
</tr>
</thead>
<tbody ng-repeat="detail in Details">
<tr>
<td>{{detail.system}}</td>
<td>{{detail.process}}</td>
</tr>
</tbody>
</table>