Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bind json to HTML table with AngularJS on page load

Tags:

angularjs

I have a simple proof-of-concept I'm using as a base to learn some AngularJS. The code displays some JSON data in an HTML table, as follows:

HTML:

<div ng-app="myApp">
    <div ng-controller="PeopleCtrl">
        <p>Click <a ng-click="loadPeople()">here</a> to load data.</p>
        <table>
            <tr>
                <th>Id</th>
                <th>First Name</th>
                <th>Last Name</th>
            </tr>
            <tr ng-repeat="person in people">
                <td>{{person.id}}</td>
                <td>{{person.firstName}}</td>
                <td>{{person.lastName}}</td>
            </tr>
        </table>
    </div>
</div>

JS:

var mockDataForThisTest = "json=" + encodeURI(JSON.stringify([
    {
    id: 1,
    firstName: "Peter",
    lastName: "Jhons"},
{
    id: 2,
    firstName: "David",
    lastName: "Bowie"}
]));


var app = angular.module('myApp', []);

function PeopleCtrl($scope, $http) {

    $scope.people = [];

    $scope.loadPeople = function() {
        var httpRequest = $http({
            method: 'POST',
            url: '/echo/json/',
            data: mockDataForThisTest

        }).success(function(data, status) {
            $scope.people = data;
        });

    };

}

A fiddle is here: http://jsfiddle.net/TUJ9D/

This works nicely; when you click the link, it calls 'loadPeople' and the json is pulled into the table. However, what I'd like to do is bind this when the page loads, so the user doesn't have to manually click the link to see the data in the table.

I wondered what the best way to do this is? Instinct is telling me to call the function with jquery on page load, but then I don't know if that's a good approach or whether Angular could do this in a better way itself.

Thanks folks.

like image 941
Dan Avatar asked Sep 12 '13 16:09

Dan


People also ask

How to bind JSON data in AngularJS?

For binding JSON data, we use ng-repeat direction which acts as for loop of the object, and curly brasses { {}} to display data field values, and hence we learned Angularjs JSON data binding. Thank you for reading, pls keep visiting this blog and share this in your network.

How to populate (bind) HTML table using AngularJS ng-repeat directive?

# Create JSON Data (which bind to our HTML Table). Here we have variable employees which hold an array of JSON objects .i.e (Employee JSON Data ), which is used to populate (bind) HTML Table using the AngularJS ng-repeat directive.

How to display JSON file data into table in HTML?

Create table in html to display json file data into it; So, visit src/app/ directory and open app.component.html and update the following code into it: Define the resolveJsonModule and esModuleInterop inside the compilerOptions object; as shown below:

How to create HTML markup with AngularJS?

Create JSON Data (which bind to Table). You can download AngularJS files from angularjs.org, or you can use Google hosted files. After importing Angularjs file, add Directive ng-app & ng-controller to the body tag. So now our HTML markup looks like as shown below # Create JSON Data (which bind to our HTML Table).


2 Answers

Just call the load function in your controller.

function PeopleCtrl($scope, $http) {

    $scope.people = [];

    $scope.loadPeople = function() {
        var httpRequest = $http({
            method: 'POST',
            url: '/echo/json/',
            data: mockDataForThisTest

        }).success(function(data, status) {
            $scope.people = data;
        });

    };

    $scope.loadPeople();
}

http://jsfiddle.net/TUJ9D/1/

like image 95
Beterraba Avatar answered Sep 27 '22 02:09

Beterraba


you can simple add ng-init="loadPeople()" directive inside your <div>.

i.e.

<div ng-controller="PeopleCtrl" ng-init="loadPeople()">
like image 33
Alfred Roa Avatar answered Sep 26 '22 02:09

Alfred Roa