greeting.id为null或$ scope.greeting为null

时间:2017-01-21 03:56:03

标签: angularjs

我测试了restful web服务,它有数据: http://localhost:8081/PetStoreSpringRest/rest/emps

这是数据: [{“id”:1,“name”:“Albert Lam”,“createdDate”:1484969286677},{“id”:2,“name”:“John John”,“createdDate”:1484969286677}]

使用谷歌浏览器调试我可以看到response.data的值 但是对于window.alert($ scope.greeting.id);它显示为空

此结果

ID为{{greeting.id}}

ID为

不确定为什么我在greeting.id中有空,$ scope.greeting.id为空

				<!doctype html>
				<html ng-app="demo">
					<head>
						<title>Hello AngularJS</title>
						<script src="libs/angular.js"></script>
				    
					<script>
					/*
					{"id":60,"content":"Hello, World!"} 
					http://localhost:8081/PetStoreSpringRest/rest/emps
					[{"id":1,"name":"Albert Lam","createdDate":1484969286677},{"id":2,"name":"John John","createdDate":1484969286677}]
					*/
					angular.module('demo', [])
					.controller('Hello', function($scope, $http) {
					    $http.get('http://localhost:8081/PetStoreSpringRest/rest/emps').
						then(function(response) {
						    $scope.greeting = response.data;
						    window.alert($scope.greeting.id);
						});
					});
					</script>
					</head>

					<body>
						<div ng-controller="Hello">
							<p>The ID is {{greeting.id}}</p>
							
							
							
						</div>
					</body>
				</html>

2 个答案:

答案 0 :(得分:0)

您似乎返回了一个数组,因此您必须执行<p>The ID is {{greeting[0].id}}</p>

之类的操作

答案 1 :(得分:0)

您的回复是......

[{"id":1,"name":"Albert Lam","createdDate":1484969286677},{"id":2,"name":"John John","createdDate":1484969286677}]

获取所有ID

<div ng-controller="Hello">
<div ng-repeat="x in greeting">
<p>The ID is {{x .id}}</p>
</div>
</div>

特定ID

<div ng-controller="Hello">
<p>The ID is {{greeting[0].id}}</p>