angularjs - get value in $scope while calling http get but its getting called once client code is finished -
in controller page i.e expense.js
$scope.getpageddataasync = function (pagesize, page) { $scope.largeload = todoservice.initialize(); todoservice.getdataasync(); $scope.setpagingdata($scope.largeload, page, pagesize); $scope.sortdata = $scope.largeload; $scope.todate = ""; $scope.fromdate = ""; //summary(); };
above page calling todoservice created in services .js
var methods = { // give reference controller array // update asyncronously var todos = []; initialize: function () { return todos; }, getdataasync: function () { var deferred = $q.defer(); // define status code error mapping $http({ method: "get", url: '/api/expensewebapi/getgriddata', cache: false }) .success(function (largeload) { todos.length = largeload.length; (var = 0; < todos.length; i++) { todos[i] = largeload[i]; } deferred.resolve(todos); }) return deferred.promise; }
you grabbing data asynchronously, rest of code going execute.
you should refactor this:
todoservice.getdataasync() .then(function(todos){ $scope.largeload = todos; $scope.setpagingdata($scope.largeload, page, pagesize); $scope.sortdata = $scope.largeload; $scope.todate = ""; $scope.fromdate = ""; });
this guarantee code execute after asynchronous call has been made.
as little code review note. can avoid loop copying items array first setting length 0
, using unshift
in conjunction apply
in order copy elements of 1 array another.
var target = []; var source = ['josh', 'bob', 'larry']; //in case target has items target.length = 0; array.prototype.unshift.apply(target, source); console.log(target); // ['josh', 'bob', 'larry']
Comments
Post a Comment