1

Can you help me please im new in angularJS,

i have a problem with Asynchrounous $http.get , i explain : i got data that i show in ngTable but in my html i got a empty table until i click to filter or to sort then i see my data.

i think its because i have a promise in my http.get. here my code to understand more ( sorry for my english)

'use strict';
(function() {
    angular
        .module('timeShareApp')
        .controller('homeController', homeController);


    function homeController($http, $scope, $filter, NgTableParams) {

    $scope.users =[];

$http.get('/api/users/').success(function(response) {
            $scope.users = response;

        });

       $scope.usersTable = new NgTableParams({
                page: 1,
                count: 10
            }, {
                total: $scope.users.length, 
                getData: function ($defer, params) {
                      $scope.data = params.sorting() ? $filter('orderBy')($scope.users, params.orderBy()) : $scope.users;
                       $scope.data = params.filter() ? $filter('filter')($scope.data, params.filter()) : $scope.data;
                       $scope.data = $scope.data.slice((params.page() - 1) * params.count(), params.page() * params.count());
                       $defer.resolve($scope.data);
                }
            });





 }

   homeController.$inject = ["$http", "$scope", "$filter", "NgTableParams"];

})();

for info : code works perfectly except that promise that i want to convert to synchonous if you can help me please.

Thank you in advance

3
  • Can you try $scope.users.concat.apply($scope.users, response); instead of $scope.users = response;? Commented Mar 15, 2016 at 23:01
  • another good solution is to load the data in a service and inject that service into your controller stackoverflow.com/questions/15161537/… Commented Mar 15, 2016 at 23:04
  • i will note that, i changed machine ( home machine to work machine (in job) and it works without changing anything) but thank you so much for your support. i note that for next data loading Commented Mar 16, 2016 at 9:44

2 Answers 2

2

In most cases, there is no reason to keep any data outside the ng-table's scope. My advice is to not modify or reference any scope variables because this can cause some pretty hard to track timing issues.

Have a look at the really good ng-table docs, which mostly have a working sample for your use case. See the docs here

Depending on the place where your filtering / sorting happens, you need to adapt this, but the following should basically work:

$scope.loadData = function() { return $http.get('/api/users/') };

$scope.usersTable = new NgTableParams(
  {
    page: 1,
    count: 10
  }, {
    total: 0, // just the inital value - will be updated later on
    getData: function ($defer, params) {
      $scope.loadData().success(function (result) {
        // assuming your result has a total and a data field...

        // update the table params with the total amount of results
        // could also be result.length...
        params.total(result.total);

        // sort and filter your data using the params object here
        var data = result.data;

        // give back the control to the table
        $defer.resolve(data);
      });
    }
  }
);

Please be aware to also set the params.total whenever your server responds. Otherwise the pagination will not be visible.

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

3 Comments

Thank you so much for your support, i just changed machine and now my code works perfectly but i note that for next devs Thank you so much
@AmenzO, can you then mark ajaegle 's answer as the accepted answer? That's the way it works on stackoverflow :).
Thank you bart for advice, last day i read comment iwant make it answered but i did not found the ajaeagle under up-down buttons :) Thank you again :)
2

I think there is no problema on adding $scope.usersTable to me defined inside your promise resolve method. Have you tried that?

Comments

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.