繁体   English   中英

如何添加第二条JSON消息?

[英]How do i add the second JSON Message?

我正在创建一个标志性应用程序,并希望能够将JSON字符串从Web服务器加载到我的应用程序中。 到目前为止,我只能加载一条消息。

![到目前为止,这是我的应用程序的样子。 我只能加载一条消息] [1]

这是我的HTML代码:

<body>
    <ion-view view-title="Announcements">
    <ion-content ng-controller="Controller">

    <ion-refresher 
      pulling-text="Pull to refresh..."
      on-refresh="doRefresh()">
    </ion-refresher>


    <ion-list>
    <ion-item>

    <div class="list card">
      <div class="item item-divider">{{announcement_name}} - {{date}}</div>
      <div class="item item-body">
        <div>
         {{message}}
        </div>
      </div>
    </div>

    </ion-item>
    </ion-list>
    </ion-content>
    </ion-view>
  </body>

这是我的JavaScript控制器:

.controller('Controller', function($scope, $http) {
    $scope.items = [1,2,3];
    $scope.doRefresh = function() {
        $http.get("")
            .success(function(data) {
                $scope.announcement_name = data.announcement_name;
                $scope.date = data.date;
                $scope.message = data.message;
            })
            .finally(function() {
           $scope.$broadcast('scroll.refreshComplete');
         });
      };
    });

这就是我的JSON文件的样子。 如您所见,我有2个数组(消息),但只有一个正在加载。 知道为什么吗?

[
    {
        "response1": {
            "announcement_name": "ExampleMessage1",
            "date": "26/05/2015",
            "message": "ExampleMessage1"
        },
        "response2": {
            "announcement_name": "ExampleMessage2",
            "date": "27/05/2015",
            "message": "ExampleMessage2"
        }
    }

]

您应该使用ng-repeat遍历json-array。

这是一个例子:

$http.get("http://www.wikicode.co.uk/announcement.json")
        .success(function(data) {
            $scope.datafromservice = data;
        })
        .finally(function() {
       ...
     });

在您的html中是这样的:

<div class="list card">
  <div class="item item-divider" ng-repeat="message in data">{{message.announcement_name}} - {{messagedate}}</div>
</div>

这是ng-repeat的文档: https : //docs.angularjs.org/api/ng/directive/ngRepeat

希望这可以帮助。

更新

您可以将完整的json粘贴到var中。 您的json可能看起来像这样(我正在开发的一个应用程序示例):

var reportapp = angular.module('reporting', []);

reportapp.controller('TreeController', ['$http', '$scope', function ($http, $scope) {
$scope.treedata = [];

$http.get('../data/tree.json').success(function (data) {
    $scope.treedata = data;
});

//After the http-call your data should look something like this:
$scope.treedata = [
{
    "name": "Titel",
    "id": "1"
},
{
    "name": "Allgemeine Standardangaben",
    "id": "2"
},
{
    "name": "Strategie und Analyse",
    "id": "3",
    "children": [
        {
            "name": "Erklärung des Entscheidungsträgers",
            "id": "4"
        },
        {
            "name": "Wichtigste Nachhaltigkeitsauswirkungen",
            "id": "5"
        }
    ]
}]
}]);

然后它可以使用ng-repeat来访问数组,例如:

<body ng-app="reporting">
<ul id="treecontainer" ng-controller="TreeController as treeCtrl">
    <li ng-repeat="knoten in treedata" id="{{knoten.id}}">{{knoten.name}}
    <ul ng-show="knoten.children">
        <li ng-repeat="kind in knoten.children" id="{{kind.id}}">{{kind.name}}</li>
    </ul>
</ul>
</body>

如果要显示子节点,可以使用ng-show

如果您有任何问题,请将其张贴在这里,我会尽力帮助您。

暂无
暂无

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

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