以下是我得到的字符串结果:
{"result":[{"sys_id":"f4425c21ec0970407e85e2a57ceff437","number":"INC0035062"}]}
这是我的代码:
$http({
method:'GET',
url:$scope.url,
transformResponse: undefined}).then(function(response) {
console.log(response.data);
}).catch(function(response) {
console.error('Error', response.status, response.data);
})
.finally(function() {
console.log("All Done");
});
那么,我怎样才能获得sys_id的单个值?
提前致谢, DP
*******编辑: Sajeetharan,仍有问题。根据您的示例更改了代码:
console.log(response.data);
$scope.data = response.data.result;
console.log($scope.data);
console.log("Sys id is:"+$scope.data[0].sys_id);
输出:
{"result":[{"sys_id":"f4425c21ec0970407e85e2a57ceff437","number":"INC0035062"}]}
undefined
TypeError: Unable to get property '0' of undefined or null reference
at Anonymous function (******.EvScripts.jsdbx:59:3)
at Anonymous function (https://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.min.js:130:399)
at m.prototype.$eval (https://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.min.js:145:96)
at m.prototype.$digest (https://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.min.js:142:165)
at m.prototype.$apply (https://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.min.js:145:399)
at l (https://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.min.js:97:248)
at K (https://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.min.js:101:373)
at y.onload (https://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.min.js:102:397)
Error undefined undefined
答案 0 :(得分:0)
如果您想访问sys_id,请访问数组的 0th index
data
,
$http.get('test.json').then(function(response){
$scope.data = response.data.result;
});
如果要使用表达式访问sys_id,
在Html中
<div ng-controller="ListCtrl">
<h1> Sys id is :{{data[0].sys_id}} </h1>
</div>
如果要使用控制器访问sys_id,
$http.get('test.json').then(function(response){
$scope.data = response.data.result;
console.log("Sys id is:"+$scope.data[0].sys_id);
});
<强> DEMO 强>
答案 1 :(得分:0)
这是最终工作的,通过angular.fromJson:
//CALL TO REST SERVICES
//---------------------
$http({
method:'GET',
url:$scope.url,
transformResponse: undefined}).then(function(response) {
console.log(response.data);
$scope.data = angular.fromJson(response.data);
console.log($scope.data['result'][0].sys_id);
}).catch(function(response) {
console.error('Error', response.status, response.data);
})
.finally(function() {
console.log("All Done");
});
//---------------------