简体   繁体   中英

Unable to pass argument to http get request in angular js

I'm pretty much new in angular js. What I am trying to do is pass an integer argument to http get request in my controller. This is how my sample code looks like.

(function() {
    angular
        .module('myApp.directory', [])

        .factory('NewsService', function($http) 
        {
            return {
                getallnews: function() {
                    return $http.get('get_all_news_feed.php?page='+pageNumber);
                }
            };
        })

        .factory('NewsFeed', function(directoryService) {
            var NewsFeed = function() {
                this.items = [];
                this.busy = false;
                this.pageNumber = 1;
            };

            NewsFeed.prototype.nextPage = function() {
                if (this.busy) return;
                this.busy = true;

                NewsService.getallnews().success(function(data) {
                    var itemData = data;

                    for (var i = 0; i < itemData.length; i++) {
                        this.items.push(itemData[i]);
                    }

                    this.pageNumber++;
                    this.busy = false;
                }.bind(this));
            };
            return NewsFeed;
        })

        .controller('MyController', function(NewsFeed, NewsService) {
            var inst = this;
            inst.news = new NewsFeed();

        });         
})();

I am building a news feed app. News is fetched from get_all_news_feed.php page and I want to pass a parameter pageNumber to it. This is while implementing infinte scrolling in angular.

I am getting undefined error. Any ideas?

Modify the factory method to accept pageNumber as parameter

getallnews: function(pageNumber) {
    return $http.get('get_all_news_feed.php?page='+pageNumber);
}

Pass it when calling the method

NewsService.getallnews(this.pageNumber)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM