title: AngularJs : prevent $http send request to server repeatedly
body: <p>I am newly with angularJS (v1.6.x). I have some problems when I worked with angularJs's $http service (GET, DELETE, PUT and POST). It was occurred after I click to any routes ( swapped route many times ) and I'm back to previous route, then make perform any action (Create, edit or delete). Is is problem in angularJS ? How should I do to prevent this recursion ?</p>
  
  <p><strong>This is What I tried :</strong></p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>app.factory('UserModel', ['$http', '$routeParams', 'env', '$q', function($http, $routeParams, env) {
              const user = {};
              user.get = get;
              user.create = create;
              user.destroy = destroy;
              user.update = update;
  
              return user;
  
  
              function create(data) {
                  var config = {
                      headers: {
                          'Accept': "applicatio/json"
                      }
                  };
  
                  return $http.post(env.APP_URL + "/api/v1/users", data, config);
              }
          }]);</code></pre>
  </div>
  </div>
  </p>
  
  <p><strong>In my Controller</strong></p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>app.controller('UserComponentControler', ['$rootScope', '$scope', 'UserModel', function($rootScope, $scope, UserModel) {
  
          $rootScope.$on('modal:on:result', function(event, result) {
  
          if (result.status) {
              if (result.resultCode === $rootScope.window.CREATE) {
                  UserModel.create(result.data).then(res =&gt; {
                      if (res.status === 200) {
                          $scope.page.load(0);
                          $scope.$emit('modal:on:create:callback', {
                              created: true
                          });
                      }
                  }, err =&gt; {
                      $scope.$emit('modal:on:create:callback', {
                          created: false,
                          message: "Some fields have error with server!"
                      });
                      console.log(err);
                  });
              } else if (result.resultCode === $rootScope.window.DELETE) {
                  userId = result.user.id;
                  UserModel.destroy(userId).then(res =&gt; {
                      if (res.status === 200) {
                          $scope.page.load($scope.page.props.current);
                          $scope.$emit('modal:on:alert', {
                              deleted: true,
                              message: {
                                  title: "Message",
                                  body: "User has beed deleted successfully!"
                              }
                          });
                      }
                      return false;
                  }, err =&gt; {
                      $scope.$emit('modal:on:alert', {
                          deleted: false,
                          message: {
                              title: "Message",
                              body: "Server Error Occured!"
                          }
                      });
                      console.log(err);
                  });
              } else if (result.resultCode === $rootScope.window.EDIT) {
                  UserModel.update(result.data, result.data.id).then(res =&gt; {
                      if (res.status === 200) {
                          $scope.page.load($scope.page.props.current);
                      }
                  }, err =&gt; {
                      $scope.$emit('modal:on:update:callback', {
                          updated: false,
                          message: "Some fields have error with server!"
                      });
                      console.log(err);
                  });
              }
          }
  
      });     
  
  }]);</code></pre>
  </div>
  </div>
  </p>
  
  <p><strong>After I swapped route from /home to /users 7 of times</strong>
  <a href="https://i.stack.imgur.com/nA0Kc.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/nA0Kc.png" alt="enter image description here"></a></p>