重复请求

时间:2017-03-23 14:54:37

标签: angularjs html5 ionic-framework

我是Ionic和angularjs的新手。获取具有http请求的控制器和服务的重复Http请求。以下是我使用ionic创建的服务之一。我没有得到我的代码中导致重复请求的内容。这只发生在带有http请求的选项卡中。

的login.html

<ion-view view-title="Login" name="login-view">
  <ion-content>
    <img src="img/epass.png" class="center" style="width: 20em; height: 18em">
    <div class="list list-inset">
        <label class="item item-input">
            <input type="text" placeholder="Username" ng-model="data.username">
        </label>
        <label class="item item-input">
            <input type="password" placeholder="Password" ng-model="data.password">
        </label>
        <button class="button button-block button-calm" ng-click="login()">Login</button>

    </div>
  </ion-content>
</ion-view>

app.js

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 && window.cordova.plugins && 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'
    })


});

位指示

.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!'
      });
    });
  }
})

服务

.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;
        }
    }
})

0 个答案:

没有答案