1

I cannot for the life of me get this json response to render in my tab-dash view for my Ionic app. The response from the local server is logged as 200 but I cannot render the json to the view! I have followed this tutorial for structuring the factory and controller http://www.benlesh.com/2013/02/angularjs-creating-service-with-http.html and referred to stackoverflow questions surrounding the topic:

  1. unable to display the http get response from the factory in controller in Angularjs application

  2. Outputting JSON Data from Local Web Server onto Ionic App using Angular

  3. Angularjs $http.get does not work

  4. Angularjs $http.get not working

I've included my controller.js, the factory responsible for executing the api call, and the template code. Thanks in advance for any help provided I appreciate it!

controller.js

(function(){

    angular.module('athenaApp.controllers', [])

        .controller('DashCtrl', function($scope, athenaApiService, $timeout) {

            athenaApiService.getArticles().then(function(data){
                $scope.news = data;
           })
      })

}());

services.js

.factory('athenaApiService', function($http){

    return{
        getArticles: function(){
            return $http.get('http://localhost:8000/athenaAPI/articles').then(function(result){
                return result.data;
            });
        }
    }
}); 

tab-dash.html

 <ion-view view-title="News">
  <ion-content>
      <ion-refresher on-refresh="doRefresh()">
      </ion-refresher>
      {{news}}
      <ion-list ng-repeat='article in news'>
        <a class="item item-thumbnail-left" href="#/tab/news/{{article.id}}"> <!-- "#/tab/news/{{article.id}}" -->
          <img src='../img/ionic.png'>
          <div class='item item-text-wrap'>
              <h3>{{article.headline}}</h3>
              <p>{{article.byline}}</p>
          </div>
        </a>
      </ion-list>
  </ion-content>
</ion-view>

server output

Listening on port: 8000
Articles Requested...
[ { id: 0,
    headline: 'Headline 0',
    byline: 'Author 0',
    datePublished: '3/4/5',
    content: { para0: 'This is a sentence', para1: 'And another sentence.' },
    originalUrl: '' },
  { id: 1,
    headline: 'Headline 1',
    byline: 'Author 0',
    datePublished: '3/5/5',
    content: { para0: 'This is a sentence', para1: 'And another sentence.' },
    originalUrl: '' },
  { id: 2,
    headline: 'Headline 2',
    byline: 'Author 0',
    datePublished: '3/6/5',
    content: { para0: 'This is a sentence', para1: 'And another sentence.' },
    originalUrl: '' },
  { id: 3,
    headline: 'Headline 3',
    byline: 'Author 0',
    datePublished: '3/7/5',
    content: { para0: 'This is a sentence', para1: 'And another sentence.' },
    originalUrl: '' } ]
GET /athenaAPI/articles 200 39.072 ms - 669
5
  • Can you post your result.data here? Open your browser dev console, check if the http request success. Also, for debugging purpose, you could add {{news}} after </ion-refresher> Commented Mar 15, 2016 at 0:54
  • Do you try console.log(data); in your controller? If there is no data and server response OK, maybe you could check CSP rules and <access> tag in config.xml. Commented Mar 15, 2016 at 1:40
  • @AwakeningByte Apparently result.data is empty because when adding {{news}} as instructed nothing appears in the view. I've added the server output to demonstrate the success of the get request and the json that is in the response. Commented Mar 15, 2016 at 2:16
  • ha ha, I get caught by this mistake many times. The result is an array. that is why result.data is empty. see my answer below Commented Mar 15, 2016 at 3:04
  • to make sure the binding is correct. add line $scope.test = "test" in controller, and add {{test}} in html. Commented Mar 15, 2016 at 3:44

2 Answers 2

1

the result is an array, that is why result.data is undefined. Just return the promise directly.

getArticles: function(){
        return   $http.get('http://localhost:8000/athenaAPI/articles');
}
Sign up to request clarification or add additional context in comments.

7 Comments

Made the change and still no luck my friend.
Yes, still completely empty. I must be doing something excessively stupid. Any other ideas about what could be going on?
do you bind to $scope or using controllerAs
bind to $scope at the moment
Then I don't see any reason why it doesn't work if the call to localhost:8000/athenaAPI/articles succeed. Last thought, replace $scope.news = data to $scope.news = [{ id: 0, headline: 'Headline 0', byline: 'Author 0', datePublished: '3/4/5', content: { para0: 'This is a sentence', para1: 'And another sentence.' }, originalUrl: '' }] this should show something on the view
|
0

Sorry vbordo, I was busy making a CodePen for you, but I found a few ways to improve what you were trying to do. <ion-list> suggests best practice by repeating via an ion-item, also I would recommend using angular.forEach to build your $scope.news array so you aren't obliterating the angular array binding.

Please see my working code sample with a sample JSON data set using your athena data service.

http://codepen.io/goneale/pen/ONbRrr?editors=1011

2 Comments

This is great @GONeale I'll definitely have to refactor according to your helpful example thanks so much!
No problem. Glad it could be of help.

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.