How do i add the second JSON Message?

smither123

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

]

Nico

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.

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

how do I add a second variable to a simple javascript function?

분류에서Dev

How do I make a script add one number to a value each second? (Lua)

분류에서Dev

How do I index from the second position to the last to the first?

분류에서Dev

How do I extract text between second quotation and first comma?

분류에서Dev

How do I add Matlab to the main menu?

분류에서Dev

How do I add a double tab in fortran?

분류에서Dev

How do I add a user in SVN?

분류에서Dev

How do I add a delay to the mouseover function?

분류에서Dev

How do I add a debug option to Makefile

분류에서Dev

How do I add this jQuery into my Javascript?

분류에서Dev

How do I know if a QuickBlox message has been throttled

분류에서Dev

How do I stop the following "External changes" message in Brackets

분류에서Dev

How do I safely JSON.stringify?

분류에서Dev

mongo store map as second class, and how to find when I do n't know the key

분류에서Dev

How do I set up a second display via DVI/viewport with a Lenovo T420S?

분류에서Dev

How do I put second element of right div to bottom of all on bootstrap?

분류에서Dev

With pandas, how do I calculate a rolling number of events in the last second given timestamp data?

분류에서Dev

Ubuntu not detecting NTFS partition on second HDD. How do I make it detect it?

분류에서Dev

How do I add a new user with specific configuration files?

분류에서Dev

How do I add Webkit webview to the Ubuntu SDK?

분류에서Dev

How do I add a network printer in Ubuntu 12.04?

분류에서Dev

How do I add my PWA to the share sheet on a mobile device?

분류에서Dev

How do I add only the positive numbers in a ruby array?

분류에서Dev

How do I handle a related models add action in cakephp?

분류에서Dev

How do I add other language in a Debian system?

분류에서Dev

How do I add conditional formatting to cells containing #N/A in Excel?

분류에서Dev

How do I add an "At Sign Search" to Firefox 68?

분류에서Dev

How do I add a regular expression to a mapping command in vim?

분류에서Dev

How do I add a Countdown Timer to my page

Related 관련 기사

  1. 1

    how do I add a second variable to a simple javascript function?

  2. 2

    How do I make a script add one number to a value each second? (Lua)

  3. 3

    How do I index from the second position to the last to the first?

  4. 4

    How do I extract text between second quotation and first comma?

  5. 5

    How do I add Matlab to the main menu?

  6. 6

    How do I add a double tab in fortran?

  7. 7

    How do I add a user in SVN?

  8. 8

    How do I add a delay to the mouseover function?

  9. 9

    How do I add a debug option to Makefile

  10. 10

    How do I add this jQuery into my Javascript?

  11. 11

    How do I know if a QuickBlox message has been throttled

  12. 12

    How do I stop the following "External changes" message in Brackets

  13. 13

    How do I safely JSON.stringify?

  14. 14

    mongo store map as second class, and how to find when I do n't know the key

  15. 15

    How do I set up a second display via DVI/viewport with a Lenovo T420S?

  16. 16

    How do I put second element of right div to bottom of all on bootstrap?

  17. 17

    With pandas, how do I calculate a rolling number of events in the last second given timestamp data?

  18. 18

    Ubuntu not detecting NTFS partition on second HDD. How do I make it detect it?

  19. 19

    How do I add a new user with specific configuration files?

  20. 20

    How do I add Webkit webview to the Ubuntu SDK?

  21. 21

    How do I add a network printer in Ubuntu 12.04?

  22. 22

    How do I add my PWA to the share sheet on a mobile device?

  23. 23

    How do I add only the positive numbers in a ruby array?

  24. 24

    How do I handle a related models add action in cakephp?

  25. 25

    How do I add other language in a Debian system?

  26. 26

    How do I add conditional formatting to cells containing #N/A in Excel?

  27. 27

    How do I add an "At Sign Search" to Firefox 68?

  28. 28

    How do I add a regular expression to a mapping command in vim?

  29. 29

    How do I add a Countdown Timer to my page

뜨겁다태그

보관