how to call $http request multiple time with different parameters in angular js

initially when the page loads below productsCtrl will be called and product details will be displayed with pagination.
when use clicks pagenumber then pageChangeHandler function will be called .how to reuse the same http call code by passing different paremeters?

initially when the page loads pageIndex is 0 then user clicks pagenumber will be passed in the same request to the server.

var app = angular.module('search', ['Pagination']);

 app.service('productService', function($http,$q){
                     
                var req = {
                    "pageIndex": 0,
                    "search": {
                        "field": "product",
                        "text": "end"
                    };

                var defer = $q.defer();

                $http.post('search',req).then(function(data){
                        defer.resolve(data);
                  });

                   this.getResponse = function(){   
                       return defer.promise;
                  }

         }).controller('productsCtrl', function ($scope,productService, $log) {

            var promise= productService.getResponse();
            promise.then(function(resp)
            {
             $scope.productList = resp.data ;
            });


            $scope.pageChangeHandler = function(pagenum) {
				console.info("pagenum:"+pagenum);
				//here need to call again above http post call by passing pageIndex with pagenum ;
			
             };


});

Open in new window

LVL 20
chaitu chaituAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Julian HansenCommented:
Your code is a bit confusing - is this right?
	var req = { // (A) WHERE IS THE CLOSING BRACKET FOR THIS?
		"pageIndex": 0,
		"search": { // (B)
			"field": "product",
			"text": "end"
		}; // <== in the middle of an object definition here - matches (B)

		var defer = $q.defer();

		$http.post('search',req).then(function(data){
			defer.resolve(data);
		});

		this.getResponse = function(){   
			return defer.promise;
		}

	}) 

Open in new window


Are you trying to write your own pagination routine? If so why not use one of the existing directives out there?
Angular Bootstrap has a pagination directive
https://angular-ui.github.io/bootstrap/

I use this pagination directive (https://github.com/michaelbromley/angularUtils/tree/master/src/directives/pagination) - it handles the ng-repeat and the pagination - the above link also discusses how to use the directive to make asynch calls and integrate into the pageChanged callback (https://github.com/michaelbromley/angularUtils/tree/master/src/directives/pagination#user-content-working-with-asynchronous-data)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Julian HansenCommented:
@chaitu chaitu

Please could you elaborate as to why you awarded a B for this question. According to EE policy the default grade is an A if not awarded then an explanation as to why not is expected.

You can find the EE policy on grading here
support.experts-exchange.com/customer/portal/articles/481419
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Angular

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.