简体   繁体   中英

How do i add the second JSON Message?

I'm creating an iconic application and want to be able to load JSON strings from a web server into my application. So far, i can only get one message to load.

![That's what my application looks like so far. I can only get one message to load][1]

Here's my HTML code:

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

Here's my javascript controller:

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

And this is what my JSON file looks like. As you can see i have 2 arrays (messages) but only one is being loaded. Any idea why??

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

]

You should loop through your json-array with ng-repeat .

Here is an example:

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

And something like this in your html:

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

Here is the doc to ng-repeat: https://docs.angularjs.org/api/ng/directive/ngRepeat

Hope, this helps.

UPDATE

You could paste the complete json in the var. Your json could look something like this (its an example from an app, I'm working on):

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"
        }
    ]
}]
}]);

Then it's able to access the array with ng-repeat like:

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

If yout want to show the children-nodes you can use ng-show :

If you have problems just post it here and I will try to help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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