2014-10-02 111 views
0

我是新来的离子型/ angularjs,我需要知道如何显示数据从一个JSON URL一个HTML视图。显示数据

所以,在我的Json URL数据是这样的:

{ 
    News: [ 
     { 
      id: "1", 
      title: " It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.", 
      image: "uploads/news/fortunaglobal_logo.jpg", 
      status: "yes" 
     }, 
     { 
      id: "2", 
      title: "fgdf", 
      description: "hhjgh", 
      image: "uploads/news/16613_10204428208286459_7484489390034618482_n.jpg", 
      status: "yes" 
     } 
     ] 
} 

我在我的脚本尝试这样做:

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

     $http.get(' "JSON URL" ') 
      .success(function(response, status, headers, config){ 

       window.alert('came in'); 
       if(status==200){ 

       $scope.news = response.data['News']; 
       window.alert($scope.news); 

       } else { 
       window.alert('No internet Connection'); 
       } 
       console.log(news); 
      }) 
      .error(function(data,status,headers,config){ 
       window.alert('error '+data+status); 
       console.log(data); 
       console.log(status); 
     }); 
}) 

这在我的html页面

<ion-view title="News" ng-controller="NewsCtrl"> 
    <ion-nav-buttons side="left"> 
     <button menu-toggle="left" class="button button-icon icon ion-navicon"></button> 
    </ion-nav-buttons> 


    <ion-content class="has-header" scroll="true" padding="true"> 




    <ul ng-repeat="newsS in news"> 
     <li>{{newsS.title}}</li> 
    </ul> 


    </ion-content> 
</ion-view> 

灿有人向我解释我做错了什么?

+0

我不熟悉的离子,而是为了让自己的代码更简单,你不必检查在'success'处理器的状态,如果你没有连接,它会进入'error' – Philipp 2014-10-02 07:35:54

+0

你能请添加更多关于您希望我们回答的信息? – magnudae 2014-10-02 07:39:09

+0

我想在我的json中使用angularjs在html视图中显示数据 – CraZyDroiD 2014-10-02 08:08:39

回答

3

既然你并不够具体,我只能猜测你想要得到什么,但我看到一些东西,可能会出现问题:

$scope.news = response.data['News']; 

如果你的描述JSON上面是全响应,这些数据应该直接包含在你的响应元素中(根据角度文档,这实际上应该是命名数据)。

那么试试这个来代替:

$scope.news = response.News; 

不能正常工作接下来的事情是

console.log(news); 

你的变量news是行不通定义,也许你想用$scope.news

+0

谢谢!有效!!! – CraZyDroiD 2014-10-02 08:21:47

+0

如果我想显示来自json中的url的图像,我应该怎么做。 – CraZyDroiD 2014-10-03 10:27:54

+0

对于单个图像的做法只是增加一个''元素,您的新闻来看,把它挂到一个'NG-IF = newsS.image'并设置其'src'属性为'SRC = {{} newS.image }' – kasoban 2014-10-04 00:52:07