Jsmidt
Jsmidt

Reputation: 139

Ionic retrieving JSON data

I have the following JSON and I am having issues in retrieving the data and displaying it in IONIC. Can someone provide me some guidance ?

JSON

mynews_JsonCallBack({
"items":[
{"headline":"Cat",
"link":"http://www.mynews.com/1",
"description":"Yellow cat",
"pubdate":"Fri, 10 Jun 2016 06:00:19",
"image":"http://www.mynews.com/1.jpg"},
{"headline":"Dog",
"link":"http://www.mynews.com/2",
"description":"Blue dog",
"pubdate":"Fri, 10 Jun 2016 06:00:19",
"image":"http://www.mynews.com/2.jpg"}
]});

Controller

.controller('NewsCtrl', function($http, $scope) {
  $scope.news = [];
  $http.get('https://www.mynews.com/.json')
    .success(function(response) {
      $scope.news.push = response.headline;
    });
})

Upvotes: 0

Views: 102

Answers (3)

Akash Chaudhary
Akash Chaudhary

Reputation: 711

Try this

$scope.news.push = response[0].headline;

instead of

$scope.news.push = response.headline;

Upvotes: 0

Mohan Gopi
Mohan Gopi

Reputation: 7724

try this

<ion list>
  <ion item ng-repeat = "title in news">
     {{title}}
  </ion item>
</ion list>

in your controller

.controller('NewsCtrl', function($http, $scope) {
 $scope.news = [];
  $http.get('https://www.mynews.com/.json')
    .success(function(response) {
      $scope.res = response.item;
        $scope.res.forEach(function(item) {
             $scope.news.push(item.headline);
         });
        console.log($scope.news);
    })
    .error(function(response){
     console.log(response);
    });

});

Upvotes: 0

Wasiq Muhammad
Wasiq Muhammad

Reputation: 3138

Try this

$scope.news = [];

.controller('NewsCtrl', function($http, $scope) {

  $http.get('https://www.mynews.com/.json')
    .success(function(response) {
      $scope.res = response.items[0];
            $scope.news.push($scope.res);
      })
    });
})

<ion-list>
  <ion-item ng-repeat="item in news">
  {{item.headline}}!
  </ion-item>
</ion-list>

Upvotes: 0

Related Questions