javascript - 使用 AngularJS 在 JSON URL 中显示数据

标签 javascript json angularjs ionic-framework

我是 ionic/angularjs 新手,我需要知道如何从 Json url 将数据显示到 HTML View 。

所以,我的 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 是完整响应,则此数据应直接包含在您的响应元素中(根据 Angular 文档,实际上应该将其命名为 data)。

所以试试这个:

$scope.news = response.News;

接下来无法工作的是

console.log(news);

你的变量news没有定义,也许你想使用$scope.news

关于javascript - 使用 AngularJS 在 JSON URL 中显示数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26156166/

相关文章:

javascript - 如何将 Promises 与 if 结合起来?

javascript - 如何在下面的 `value: _value(this.from, this.to, line)` 行中获得 DRY?

javascript - 启动ajax调用observer并监听特定url获取url参数

java - 使用 java 使用给定的键解析复杂的 json 和 xml,无需使用 pojo

javascript - FS 文件观察器,获取更改

javascript - 如何在 JavaScript 函数中使用 Angular 变量作为参数

javascript - 在 AngularJS 中动态生成 ng-model

javascript改变元素背景

java - 如何在流查询(Java)中使用 JSON 数组作为 Kafka 记录?

javascript - AngularJS使用mvc在spa模型中路由无限循环