NilmeX
NilmeX

Reputation: 63

Getting JSON from nodejs in Angular

So I couldn't find my answer anywhere. Using node I got everything from database, and I outputted it into the file. It's simple JSON data, chat logs. I can access it though my browser. Looks like this:

[{
  "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"
}]

Etc..

Now how can I get this data by angular? I tried $http.get but I think I messed something up... I'm new to angular and node, so if someone can explain it to me I will be happy.

Upvotes: 2

Views: 49

Answers (1)

Muhammad Soliman
Muhammad Soliman

Reputation: 23866

<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>

Upvotes: 1

Related Questions