Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them, it only takes a minute:

I am building a quick Angular app that uses a service to grab a JSON from a URL.

The JSON structure looks like this:

{news:[{title:"title",description:'decription'},
       {title:"title",description:'decription'},
       {title:"title",description:'decription'}
      ]};

What I need is just the array within the news object.

My code to import the JSON is as follows:

app.factory('getNews', ['$http', function($http) { 
  return $http.get('URL') 
        .success(function(data) { 
          return data; 
        }) 
        .error(function(err) { 
          return err; 
        }); 
}]);

Then to add the data to the $scope object in the controller I do this:

app.controller('MainController', ['$scope','getNews', function($scope, getNews) {
getNews.success(function(data)) {
    $scope.newsInfo = data.news;
    });

});

But it doesn't work. When I load the html page, there is only white. My thinking is that this is because it isn't grabbing the array within the JSON and using that data to populate the HTML directive I set up.

newsInfo.js:

app.directive('newsInfo',function(){
  return {
    restrict: 'E',
    scope: {
  info: '='
},
templateUrl:'newsInfo.html'
  };
    });

newsInfo.html:

<h2>{{ info.title }}</h2>
<p>{{ info.published }}</p>

My HTML doc is:

<head>      
    <title></title>

    <!--src for AngularJS library--> 
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.5/angular.min.js"></script>
</head>

<body ng-app="THE APP NAME"> <!--insert ng app here-->
    <div ng-controller="MainController"> <!--insert ng controller here-->
        <div ng-repeat="news in newsInfo"><!--insert ng repeat here-->
            <!-- directive goes here-->
            <newsInfo info="news"></newsInfo>
        </div>
    </div>



    <!-- Modules -->
    <script src="app.js"></script>

    <!-- Controllers -->
    <script src="MainController.js"></script>

    <!-- Services -->
    <script src="getNews.js"></script>

    <!-- Directives -->
    <script src="newsInfo.js"></script>
</body>

Thoughts?

share|improve this question
    
provide the code of html – Anik Islam Abhi Aug 13 at 2:14
    
Posting all code is relevant. Also, put something non related into the html will tell you something as well. If it's still white, has nothing to do with the service. – Tj Gienger Aug 13 at 2:17
    
without seeing your HTML page it's hard to say exactly what's occurring, but my first guess is that angular fails to load, because you have an error in your code. the way you are calling your factory here seems strange to me. – Claies Aug 13 at 2:18
    
so with that update, it's clear that if newsInfo is empty, you will not display anything. Are you getting any error messages that might explain why newsInfo would be empty? – Claies Aug 13 at 2:21
    
posted the HTML. If I replace the directive with a normal div and a test <p>, it shows up. So I guess it's on Angular's end. Also, what about the way I call my factory is weird? – broderickga Aug 13 at 2:21

4 Answers 4

up vote 0 down vote accepted

While I am answering my own question, I should point out that everyone's answers were also correct. The code had multiple errors, but the final error was fixed by doing the following:

Apparently I had to upload the newsInfo.html doc to an S3 bucket and use that URL as “Cross origin requests are only supported for HTTP.” But then Angular blocked that external source with the Error: "$sce:insecurl Processing of a Resource from Untrusted Source Blocked".

So since the html template was so simple, i just bipassed this issue by typing in the template directly into the .js directive and it worked! thanks everyone for the help!

share|improve this answer
$scope.getNews = function(){
    return $http.get('URL').success(function(data) { 
      return data.news; 
    })
};

^ This, just change it so that the success function returns only the news!

share|improve this answer

Change

<newsInfo info="news"></newsInfo>

to

<news-info info="news"></news-info>

example: https://jsfiddle.net/bhv0zvhw/3/

share|improve this answer

You haven't specified the controller.

<div ng-controller="MainController"> <!--insert ng controller here-->
    <div ng-repeat="news in newsInfo"><!--insert ng repeat here-->
        <!-- directive goes here-->
        <newsInfo info="news"></newsInfo>
    </div>
</div>
share|improve this answer
    
wow you were right. still didn't work but an obvious error. – broderickga Aug 13 at 2:27
    
Make sure you are assigning correct data to the variable, as Martin Ciz suggested in the other answer $scope.newsInfo = data.news; – MjZac Aug 13 at 2:28

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.