繁体   English   中英

使用AngularJS在JSON URL中显示数据

[英]Display data in JSON URL using AngularJS

我是ionic / angularjs的新手,我需要知道如何从Json网址向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>

有人可以向我解释我做错了什么吗?

由于您不够具体,所以我只能猜测您要尝试的内容,但是我看到一些可能有问题的事情:

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

如果上面描述的JSON是完整响应,则此数据应直接包含在您的response元素中(根据角度文档,它实际上应被命名为data)。

因此,请尝试以下操作:

$scope.news = response.News;

下一件行不通的是

console.log(news);

您的可变news没有定义,也许您打算使用$scope.news

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM