离子应用程序 - 控制器构造

时间:2015-06-23 12:01:38

标签: javascript angularjs login controller ionic

我是Ionic的新手,我是一个移动应用程序项目,现在我在控制器登录时被阻止了一段时间。你能帮助我,告诉我我做错了什么。

艾玛

<!--UserService.js-->

  function UserService($http, $localStorage, $location, $log, $q) {
    return {
        connectionStatus: "",
        login: function(username, password) {
            $log.log('connecting...');
            return $http.post(
                'http://coeurro.com/vistagram_plateform_sf2/web/mobile/api/login_check',
                '_username='+username+'&_password='+password,{headers: {'Content-Type': 'application/x-www-form-urlencoded'}}
            ).then(function(response){
                    if (response.data.token) {
                        $log.log('login success');
                        $localStorage.currentUser = {
                            'token': response.data.token,
                            'username': username,
                            'lastConnection': response.data.data.lastConnection
                        };
                    } else {
                        $log.log('login failed - '+response.error);

                    }
                    return response.data;
                })
        },
        logout: function() {
            $log.log('logout');
            $localStorage.currentUser = null;
            $location.path( "/login" );
        },
        isLoggedIn: function() {
            return $localStorage.currentUser != null;
        },
        currentUser: function() { return $localStorage.currentUser }
    };
}

angular
    .module('lunilac')
    .factory('UserService', UserService);

这是UserService代码的一部分,知道我使用标记进行连接

<!--ConnectionController.js-->

function ConnectionController($scope, $ionicPopup, $state, UserService) {
    $scope.data = {};

    $scope.login = function() {
        UserService.login($scope.data.username, $scope.data.password).success(function(data) {
            $state.go('app.home');
        }).error(function(data) {
            var alertPopup = $ionicPopup.alert({
                title: 'Login failed!',
                template: 'Please check your credentials!'
            });
        });
    }    
}

angular
    .module('lunilac')
    .controller('ConnectionController', ConnectionController);
<ion-view view-title="" name="login-view">
  <ion-content class="padding">    
    <form ng-submit="ConnectionCtrl.login()">
      <img src="img/mezi.png" class="padding">
      <div class="connex">
        <label class="item item-input user">      
          <input type="text" placeholder="Username" ng-model="data.username"required>
        </label>
        <label class="item item-input pass">      
          <input type="password" placeholder="Password" ng-model="data.password"required>
        </label>
        <label class="item padding">
          <button class="button button-block button-positive" ng-click="login()">Login</button>
        </label>
      </div>
    </form>    
  </ion-content>
</ion-view>

0 个答案:

没有答案