javascript - 在 Angular 中从 nodejs 获取 JSON

标签 javascript angularjs json node.js

所以我无法在任何地方找到我的答案。使用 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/

相关文章:

javascript - 调用 Array.prototype.slice 时 Firefox 错误 'setting a property that only has a getter'

javascript - 调用Array.map()中的类方法

javascript - 如何使用 Javascript 基于另一个 div 设置 div 的样式?

javascript - Bootstrap 4 在折叠中更改图标

javascript - Highcharts 工具提示格式与系列不同

html - 使用 angularjs 的 Elasticsearch 搜索数据

javascript - Firefox 浏览器缓慢加载 AngularJS 应用程序

angularjs - angular1.x中的$ promise和$ q promises有什么区别?

javascript - 我如何将此 AJAX json 结果(具有键/值属性)转换为 javascript 数组

javascript - D3力布局仅显示一个链接