0

i have page that contains 3 controller

and i have service that make $http call

after get the object from the http call i want the other controller use the data from the server.

for example my main controller apply the http call and the other controller use the response for the call with no multiple server call.

this is my print screen for the application the "loading" in this page is the main controller but the notify is another controller and the status is another controller. the both controllers need the data from the call

what the best way to apply this call? thanks

my NOT FINISHED CODE

App.factory('siteService', function($http, $location, $q) {
    var siteObject = {},defered = $q.defer();
    function applaySiteServiceCall() {        
        $http(req).success($q.resolve);
        return defered.promise;
    }
    return {
        applaySiteServiceCall : applaySiteServiceCall,
        promise : defered.promise
    };
});
App.controller('VerifyController', ['$scope','$sce' ,'siteService', function($scope, $sce, siteService) {
    siteService.applaySiteServiceCall().then(function (data) {
        siteService.siteObject = data;
ֿֿֿֿ/// needed apply the data in the other controller
    });

}]);

//// controller 2

App.controller('AlertController', ['$scope','siteService', function($scope, siteService) {
    siteService.siteObject.promise.then(function(data) {
        $scope.siteObject = data;
    });
}]);
3
  • I think if you add your response object to the scope, then it should be available to your other controllers via the scope object. Commented May 16, 2015 at 21:11
  • 1
    Please provide your code Commented May 16, 2015 at 21:13
  • What is the relationship between the controllers? Is one the parent of the other? Can you show how the markup looks? Commented May 16, 2015 at 22:51

1 Answer 1

1

If you want to load data from server at once and use it several times, you can use cache option

angular.module('app', []).factory('service', function($http) {
    return {
        load: function() {
            return $http.get('/api/url', {cache: true})
        }
    }
});

Then you can call service.load() and request will be performed only at first time, later calls will return same result without extra requests.

See $http docs about kinds of caching and more.

Sign up to request clarification or add additional context in comments.

1 Comment

sorry, but i don't want to use cache i want use the data same page multi controllers, thanks

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.