我正在尝试从 REST 服务获取数据并在浏览器中显示元素。 一段代码:
Server.prototype.typeNames = function() {
var json = '{'
+ '"query" : "MATCH n RETURN n.typeName LIMIT 10"'
+ '}';
$http.post("http://localhost:7474/db/data/cypher", json).then(function(response) {
var data = response.data;
console.log(data);
return data;
});
}
我确信 JSON 和 REST 调用是正确的,因为我可以在控制台中看到数据并且它是正确的,但我在页面上看不到它。 我的 js 文件中也有这个:
$scope.typeNames = Server.typeNames()
在 HTML 中:
{{typeNames}}
另外:我为 chrome 安装了 AngularJS 调试扩展,但它也是空的:
typeNames: null
在这里(http://pastebin.com/itHv97da)您可以找到更大的代码部分。这段代码不是我的,Server.prototype 的东西都在那里。我刚刚添加了 Server.prototype.typeNames,我正在尝试让它工作。
最佳答案
您的 typeNames
函数没有返回任何内容。
根据版本,您可以尝试返回整个 $http 部分,但更好的方法是返回一个 promise ,并在 .then
回调中解决该 promise 。
类似这样的:
var deferred = $q.defer();
$http.post("http://localhost:7474/db/data/cypher", json).then(function(response) {
var data = response.data;
console.log(data);
deferred.resolve(data); // <--- this is the key
return data;
});
return deferred.promise;
关于javascript - AngularJS 进行 REST 调用并在浏览器中显示结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24382909/