0

I'm pretty new to angular js but it seems like my simple code should work. Here is the html:

    <body ng-app="MyHomepage">
    <div ng-controller="RedditLoad">
        {{a}}
        <ul>
            <li ng-repeat="article in a">
            {{article.data.title}}
            </li....

and here is my angualr_app.js:

    var App = angular.module('MyHomepage', [])
    function RedditLoad($scope){
        $.getJSON("http://www.reddit.com/.json?jsonp=?", function(data) {
            var data_array = []; 
            $.each(data.data.children, function(i,item){
                data_array.push(item);
            });
           console.log(data_array);
           $scope.a = data_array;
        });
    }

What am I doing wrong? console.log(data_array); is showing the correct values but the data wont seem to get passed to the template.

0

1 Answer 1

1

The getJSON callback isn't executed in the angular context so angular doesn't know about your changes and won't refresh the bindings. When code is called from an external source (like a jQuery event ), you have to encapsulate your scope changes in an $apply call:

$scope.$apply(function{ 
    $scope.a = data_array;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. That explains alot. This fixed the issue.

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.