angularjs - How to stop $http being called multiple times in an Angular controller -


inside controller have tried stop $http being called multiple times, efforts seem in vein.

i want report's items loaded once.

have tried checking items being undefined , having timesrun variable @ top of controller , increasing 1 @ bottom of it.

if ($scope.items === undefined && $scope.timesrun == 0) {     var req = {       method: 'get',             *snipped*         };      $http(req).success(function (data, status, headers, config) {       $scope.items = data;     }).error(function (data, status, headers, config) {       sweetalert.swal("error " + status, data, "error");     }); } 

i have had in service service gets called multiple times.

i'm missing trick. can understand digest cycle , have expressions in page need checked can see why controller running multiple times, cannot understand how can exclude web calls after they've run once.

being called $http service twice or more may due many reasons. few listing below:

  1. do not mention controller (see : omit ng-controller='homepanel' in html) name in template.

if using ngroute, , mentioning controller name in template, in route config, may calling twice. example. in app.js

        app.config([ '$routeprovider', '$sceprovider',         function($routeprovider, $sceprovider) {             $routeprovider.when('/', {                 templateurl : 'home',                 controller : 'homepanel',             });       }]); 

and in html:

<script type="text/ng-template" id="home">     <div ng-controller="homepanel">     </div> </script> 
  1. don't let ajax call cache. append random value in end of url.

    var req = {
    method: 'get',
    url : someurl + "&random="+ math.random();
    };

hope may help.


Comments

Popular posts from this blog

Fail to load namespace Spring Security http://www.springframework.org/security/tags -

sql - MySQL query optimization using coalesce -

unity3d - Unity local avoidance in user created world -