从angularjs REST服务使用JSON对象
我有一个REST服务返回JSON用一个号码
I have a rest service returning JSON with one number
{"uptime":"44"}
在网址可供选择:
available under url:
http://localhost/uptime
和我想显示使用页面angularJS此值。
and I want to display this value on page using angularJS.
我写了一个资源,从这个休息URL数据:
I wrote a resource to get data from this rest url:
angular.module('utilService', ['ngResource']).
factory('UtilService', function($resource) {
var UtilService = $resource('/uptime', { },
{
'get' : { method: 'GET', params: { format: '.json' } , isArray : false }
}
)
UtilService.loadUptime = function(cb) {
return UtilService.get();
}
return UtilService;
});
但是,当我使用它的控制器:
But when I use it in Controller:
angular.module('log', ['logService', 'utilService']);
function UptimeCtrl($scope, UtilService) {
var time = UtilService.loadUptime();
$scope.uptime = time;
}
我所有我的网页上看到的是{正常运行时间:44}
all I see on my page is {"uptime":"44"}
我想我需要提取/接收的对象转换成JSON并获得它的正常运行时间属性。但我想JSON.parse,time.uptime,时间['的正常运行时间']和没有成功。
I guess I need to extract/convert received object to JSON and get it 'uptime' property. but I tried JSON.parse, time.uptime, time['uptime'] and without success.
我要么做一些完全错误或遗漏这里的一些基本问题。
I am either doing something completely wrong or missing some basic issue here.
$资源
只会返回任何对象由服务器返回的,你不能真正做任何而使用$资源数据pre /后处理。但即使没有解析数据,你应该能够显示你所需要的。以你的code例如 {{} uptime.uptime}
前pression在模板中应该做的伎俩。
$resource
will just return whatever object is returned by a server, you can't really do any data pre/post processing while using $resource. But even without parsing data you should be able to display what you need. Taking your code example the {{uptime.uptime}}
expression in a template should do the trick.