简体   繁体   English

使用AngularJS在JSON URL中显示数据

[英]Display data in JSON URL using AngularJS

I'm new to ionic/angularjs and I need to know how to display data to a HTML view from a Json url. 我是ionic / angularjs的新手,我需要知道如何从Json网址向HTML视图显示数据。

So, data in my Json URL looks like this: 因此,我的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"
        }
      ]
}

I tried this in my script: 我在我的脚本中尝试过:

.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);
         });
})

And this in my html page 这在我的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>

Can someone explain to me what I am doing wrong? 有人可以向我解释我做错了什么吗?

Since you are not specific enough I can only guess what you're trying to get at, but I see some things that might be problematic: 由于您不够具体,所以我只能猜测您要尝试的内容,但是我看到一些可能有问题的事情:

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

If your described JSON from above is the full response, this data should directly be contained in your response element (which is actually supposed to be named data according to the angular documentation). 如果上面描述的JSON是完整响应,则此数据应直接包含在您的response元素中(根据角度文档,它实际上应被命名为data)。

So try this instead: 因此,请尝试以下操作:

$scope.news = response.News;

Next thing that can't work is 下一件行不通的是

console.log(news);

Your variable news is nowhere defined, maybe you meant to use $scope.news ? 您的可变news没有定义,也许您打算使用$scope.news

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

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