簡體   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