Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS loading data on page load

I'm new to AngularJS and trying to setup an app that reads an RSS feed and displays the items upon page load.

I have figured out how to have the data loaded and displayed upon clicking a link (see fiddle), but can't figure out how to have this happen on page load.

My thought is that I'm not using $scope properly or should be using a model.

Any help is appreciated.

Code: http://jsfiddle.net/mikeyfreake/Lzgts/312/

like image 730
Michael Freake Avatar asked Dec 19 '22 04:12

Michael Freake


2 Answers

Just call the method $scope.loadFeeds() after defining the method itself.

like image 78
Shashank Agrawal Avatar answered Jan 02 '23 20:01

Shashank Agrawal


you have not called your controller function. just use this code for controller:

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

    app.controller('FeedController', ['$scope', 'FeedService', function ($scope, Feed) {

        console.log('FeedController called.');

        //These calls cause errors:
        //$scope.loadFeeds();
        //loadFeeds();
        //this.loadFeeds();
        //loadFeeds();

        $scope.loadFeeds = function () {
            console.log('loadFeeds called.');

            Feed.parseFeed('http://www.rotoworld.com/rss/feed.aspx?sport=nfl&ftype=article&count=12&format=atom').then(function (res) {
                $scope.rotoWorld = res.data.responseData.feed.entries;
            });
        };
        $scope.loadFeeds();//you have leave this line pf code
    }]);
like image 36
Mukund Kumar Avatar answered Jan 02 '23 18:01

Mukund Kumar