How to read data from JSON to ANGULARJS controller

13 views
Skip to first unread message

נתי שטרן

unread,
Sep 16, 2019, 2:07:02 PM9/16/19
to Angular and AngularJS discussion
I want to read data from JSON that located on SERVER to this code:


<div ng-app="myApp" ng-controller="myCtrl">
//output
</div>
<script>
var app = angular.module("myApp"[]);
app.controller("myCtrl"function($scope) {
 //read data from JSON
});
</script>

how  to do this?????
 --
netanel

Sander Elias

unread,
Sep 17, 2019, 6:31:30 AM9/17/19
to Angular and AngularJS discussion
Hi נתי שטרן,

Oh, its over 2 years ago I did AngularJs, but something like this should work:

angular.module('plunker', []).controller('MainCtrl', function($scope,$http) {
 $scope
.name = 'Plunker';
 $http
.get('https://jsonplaceholder.typicode.com/users/1')
 
.then(resp => resp.data)
 
.then(user => $scope.user = user)
});

then use user in template
See it working in this plnkr

נתי שטרן

unread,
Sep 17, 2019, 10:28:26 AM9/17/19
to Angular and AngularJS discussion

do you use in Arrow functions?

--
thanks
netanel
בתאריך יום שלישי, 17 בספטמבר 2019 בשעה 13:31:30 UTC+3, מאת Sander Elias:
Reply all
Reply to author
Forward
0 new messages