4

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/

3 Answers 3

5

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

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

2 Comments

Damn that was easy. I was attempting the call prior to defining the method. I assumed that when the JS file was parsed, the loadFeeds method would be defined, but obviously it wasn't.
Yes, it will be a normal javascript execution.
4

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
    }]);

Comments

2

Use ng-init directive that will call exactly after controller gets loaded & make much more impact while testing a code using Karma & Jasmine.

Markup

<div ng-controller="FeedController" ng-init="loadFeeds()">
  ..other html here
<div>

Fiddle Here


Though the better way would be calling loadFeeds method from controller at the end to ensure all the variables & methods have been initialized. You have to mock all the ajax response as mock data.

1 Comment

@MichaelFreake you will really know how important it is while doing the code testing..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.