所以我无法在任何地方找到我的答案。使用 Node 我从数据库中获取了所有内容,并将其输出到文件中。它是简单的 JSON 数据,聊天记录。我可以通过我的浏览器访问它。看起来像这样:
[{
"id":"1b4f0d8b-4577-493e-adde 68a9a96f647d",
"message":"Hi",
"name":"NilmeX",
"timestamp":"18:11"
},
{
"id":"4b4f2dc1-7162-484b-98b4-dbdbc751dff4",
"message":"Hi2",
"name":"NilmeX",
"timestamp":"18:11"
}]
等等
现在如何通过 Angular 获取这些数据?我尝试了 $http.get
但我想我搞砸了......我是 angular 和 Node 的新手,所以如果有人能向我解释它我会很高兴。
最佳答案
<div ng-app="myApp" ng-controller="myCtrl">
<p ng-repeat="item in myArray">{{item.id}} - {{item.name}}</p>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope, $http) {
$http.get("service endpoint from nodejs")
.then(function(response) {
$scope.myArray = response.data;
});
});
</script>
关于javascript - 在 Angular 中从 nodejs 获取 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44488101/