0

Hello everyone I am trying to bind a table in angulajs. I am getting data from database in table and trying to fill that table. Here I did

$scope.getPageDetails = function () {
    $http.get('/api/privilege/getpagedetails/')
        .success(function (data, status) {
            $scope.Page = data;
            console.log($scope.Page);
        }).error(function () {
        });
}

then i get the data in $scope.Page which is defined as $scope.Page = []; here i am attaching the console image that i am getting data in $scope.pageenter image description here

and here is my table

<table class="table table-bordered table-hover">
                    <thead>
                        <tr>
                            <th>
                                ID
                            </th>

                            <th>
                                PageName
                            </th>

                            <th>
                                PageUrl
                            </th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr ng-repeat="Page in Pages">
                            <td>{{Page.id}}</td>
                            <td>{{Page.pageName}}</td>
                            <td>{{Page.pageUrl}}</td>
                            <td>
                                <input type="checkbox"  value="Edit" />
                            </td>
                        </tr>
                    </tbody>
                </table>

please help where I am missing I will be very thankful to you all.

1
  • $scope.Page should be $scope.Pages Commented Dec 7, 2016 at 14:41

4 Answers 4

1

In your view you are iterating over Pages, but I dont see it defined in your controller.

Change

$scope.Page = data;

to

$scope.Pages = [data];

Final Code HTML:

<table class="table table-bordered table-hover">
                <thead>
                    <tr>
                        <th>
                            ID
                        </th>

                        <th>
                            PageName
                        </th>

                        <th>
                            PageUrl
                        </th>
                    </tr>
                </thead>
                <tbody>
                    <tr ng-repeat="Page in Pages">
                        <td>{{Page.id}}</td>
                        <td>{{Page.pageName}}</td>
                        <td>{{Page.pageUrl}}</td>
                        <td>
                            <input type="checkbox"  value="Edit" />
                        </td>
                    </tr>
                </tbody>
            </table>

controller:

$scope.getPageDetails = function () {
$http.get('/api/privilege/getpagedetails/')
    .success(function (data, status) {
        $scope.Pages = [data];
    }).error(function () {
    });

}

EDIT: if your data is an object make it an array, so ng-repeat can iterate over it

Sign up to request clarification or add additional context in comments.

6 Comments

what should i have to do in n-repeat
If you point to the correct reference in your controller (changing $scope.Pages = data;), no work needed on the ng-repeat. Angular will do the rest
Not woking sir i changes the page to pages but not binding in ng-repeat
What is the content of data if it just an object you can wrap it within an array, because ng-repeat works with arrays something like $scope.Pages = [data]; in order to work, but If this will be used only once I dont see the use of using ng-repeat, you could just build the table with the object.
I did this<tbody> <tr ng-repeat="Pages in Page"> <td>{{Pages.id}}</td> <td>{{Pages.pageName}}</td> <td>{{Pages.pageUrl}}</td> <td> <input type="checkbox" value="Edit" /> </td> </tr> </tbody> But this is not binding..
|
1
$scope.getPageDetails = function () {
    $http.get('/api/privilege/getpagedetails/')
        .success(function (data, status) {
            $scope.Pages = data; //change page to pages in whole code,  
                                 //no need to change any thing in html page
            console.log($scope.Pages);
        }).error(function () {
        });
}

5 Comments

what you are getting here is normal object with one key and value json.
ng-repeat works on array of objects.. here you can directly bind the $scope with the view.
$scope.getPageDetails = function () { $http.get('/api/privilege/getpagedetails/') .success(function (data, status) { $scope.Page = data; //sorry dont change here remove ng- //repeat from the html file console.log($scope.Page); }).error(function () { }); }
<table class="table table-bordered table-hover"><thead><tr><th>ID</th><th>PageName</th><th>PageUrl</th></tr></thead><tbody><tr><td>{{Page.id}}</td><td>{{Page.pageName}}</td><td>{{Page.pageUrl}}</td><td><input type="checkbox" value="Edit" /></td></tr></tbody></table>
@Gaurav_0093 use ng-model in the td tag like
0

$scope.Page in controller vs Pages in your HTML ng-repeat binding, you need to write $scope.Pages :)

Comments

0

Change $scope.Page = data to $scope.Pages = data.

1 Comment

yes I changed it according to you but what should i Do in ng-repeat

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.