title: Duplicate requests
body: <p>I am new to Ionic and angularjs. Getting duplicate Http requests for the controllers and services having http request. The following is one of the service I have created using ionic. I am not getting what's causing duplicate request in my code. This is only happening to the tabs with http requests.</p>
  
  <p><strong>login.html</strong></p>
  
  <pre><code>&lt;ion-view view-title="Login" name="login-view"&gt;
    &lt;ion-content&gt;
      &lt;img src="img/epass.png" class="center" style="width: 20em; height: 18em"&gt;
      &lt;div class="list list-inset"&gt;
          &lt;label class="item item-input"&gt;
              &lt;input type="text" placeholder="Username" ng-model="data.username"&gt;
          &lt;/label&gt;
          &lt;label class="item item-input"&gt;
              &lt;input type="password" placeholder="Password" ng-model="data.password"&gt;
          &lt;/label&gt;
          &lt;button class="button button-block button-calm" ng-click="login()"&gt;Login&lt;/button&gt;
  
      &lt;/div&gt;
    &lt;/ion-content&gt;
  &lt;/ion-view&gt;
  </code></pre>
  
  <p><strong>app.js</strong></p>
  
  <pre><code>angular.module('starter', ['ionic','ngCordova', 'starter.controllers', 'starter.services'])
  
  .run(function($ionicPlatform) {
    $ionicPlatform.ready(function() {
      // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
      // for form inputs)
      if (window.cordova &amp;&amp; window.cordova.plugins &amp;&amp; window.cordova.plugins.Keyboard) {
        cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
        cordova.plugins.Keyboard.disableScroll(true);
  
      }
      if (window.StatusBar) {
        // org.apache.cordova.statusbar required
        StatusBar.styleDefault();
      }
    });
  })
  
  .config(function($stateProvider, $urlRouterProvider) {
  
    // Ionic uses AngularUI Router which uses the concept of states
    // Learn more here: https://github.com/angular-ui/ui-router
    // Set up the various states which the app can be in.
    // Each state's controller can be found in controllers.js
    $stateProvider
    .state('splash', {
          url: '/splash',
          templateUrl: 'templates/splash.html'
      })
    // setup an abstract state for the tabs directive
      .state('tab', {
      url: '/tab',
      abstract: true,
      templateUrl: 'templates/tabs.html'
    })
  
    //Login functionality
    .state('login', {
          url: '/login',
          templateUrl: 'templates/login.html',
          controller: 'LoginCtrl'
      })
  
  
  });
  </code></pre>
  
  <p><strong>contoller</strong></p>
  
  <pre><code>.controller('LoginCtrl', function($scope, User, LoginService, $ionicPopup, $state) {
    $scope.data = {};
    $scope.login = function() {
      LoginService.loginUser($scope)
      .success(function(data) {
        //TODO What is in the data object?
        console.log('response.data.token_id in controllers:'+LoginService.res);
        console.log('login success. ' + $scope.data.token );
        console.log('$scope.data.username: '+$scope.data.username);
        User.cwusername=$scope.data.username;
            User.updateToken($scope.data.token);
        var token=$scope.data.token;
  
        //User.update(data);
        $state.go('tab.dash');
  
      })
      .error(function(data) {
        const alertPopup = $ionicPopup.alert({
          title: 'Login failed!',
          template: 'Please check your credentials!'
        });
      });
    }
  })
  </code></pre>
  
  <p><strong>service</strong></p>
  
  <pre><code>.factory('User', function(){
    var user ={};
    var token = 0; 
    var updateToken = function(id) {
      this.token = id;
    };
    var getToken = function() {
      return token;
    };
    return {
      loggedIn: function() {
        return user;
      },
      updateToken: updateToken,
      update: function(user) {
        this.user = user;
      },
      getToken: getToken 
    };
  })
  
  .service('LoginService', function($q, $http) {  
  
      var url = 'hardcoded url';
  
      var id = 0;
      return {
          loginUser: function($scope) {
              var deferred = $q.defer();
              var promise = deferred.promise;
              console.log('scope data.username=' + $scope.data.username );
              console.log('scope data.password=' + $scope.data.password );
              var dataobj = {'username': $scope.data.username, 'login_pwd': $scope.data.password};
  
              var restRespond = "";
  
              console.log('calling for json');
              var res = $http.get(url + '/' + $scope.data.username + '/' + $scope.data.password)
                  .then(function(response) {
                  console.log('response.data.id in service:'+response.data.id);
                  id = response.data.id;
                  restRespond = response.data.response_msg;
                  console.log(restRespond);
                  if (id != 0) {
                      $scope.data.token = id;
                      console.log('Welcome ' + name + '!');
                      deferred.resolve('Welcome ' + name + '!');
                  } else {
                      console.log('Wrong credentials.' +  id);
                      deferred.reject('Wrong credentials.' +  id);
                  }
              });
  
              promise.success = function(fn) {
                  promise.then(fn);
                  return promise;
              }
              promise.error = function(fn) {
                  promise.then(null, fn);
                  return promise;
              }
              return promise;
          },
          loginToken: function() {
              return id;
          }
      }
  })
  </code></pre>