1

for example I want to view data from two urls: 1- http://jsonplaceholder.typicode.com/posts/ 2- http://jsonplaceholder.typicode.com/todos/

Can I load data from these two url's in one app with two controllers in one view page?

1 Answer 1

1

Use $http.get to issue the query (JSFiddle):

angular.module('Joy', [])
.controller('CtrlOne', ['$http', '$scope', function ($http, $scope) {
    $http.get('http://jsonplaceholder.typicode.com/posts/').then(function (data) {
        $scope.posts = data.data;
    });
}])
.controller('CtrlTwo', ['$http', '$scope', function ($http, $scope) {
    $http.get('http://jsonplaceholder.typicode.com/todos/').then(function (data) {
        $scope.todos = data.data;
    });
}]);

HTML:

<div ng-app="Joy">
    <div ng-controller="CtrlOne">{{ posts[0] | json }}</div>
    <div ng-controller="CtrlTwo">{{ todos[0] | json }}</div>
</div>
Sign up to request clarification or add additional context in comments.

3 Comments

jsfiddle.net/soulsurfer/q47070rn/1 I've like updated your jsfiddle, my alteration doesn't seems to work, can you please explain me whats happening here? and change the jsfiddle.
@user2317954 Check the console to find error: XMLHttpRequest cannot load http://api.mvflights.com/arrivals/. No 'Access-Control-Allow-Origin' header is present on the requested resource.. Well, it is another issue of Access-Control-Allow-Origin
ah thanks a lot, I'll look in to this issue right away.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.