0

I am trying to get an angular animation to be triggered upon an unsuccessful login (either when the server sends a 4xx (e.g. 403) or when no credentials are provided).

Here is the controller:

 .controller('SigninCtrl', ['$scope', '$rootScope', '$cookies', '$state', '$animate', 'signinService', function ($scope, $rootScope, $cookies, $state, $animate, signinService) {
        $scope.signin = function () {
            $scope.shake = false;
            if ($scope.credentials) {
                signinService.signin($scope.credentials, function (status, memberRole) {
                        $scope.shake = false;
                        //TODO: necessary to check status?
                        if (status === 200) {
                           ...
                        }
                    },
                    function () {
                        $scope.shake = true;
                    });
            }
            else {
                //TODO: only shakes/works once!
                $scope.shake = true;
            }
        }
    }]);

The form:

<form class="form-signin" ng-class="{shake: shake}" name="signinForm" ng-submit="signin()" novalidate>
    <h2 class="form-signin-heading signin-title">{{'SIGNIN' | translate}}</h2>
    <input type="email" ng-required name="username" ng-model="credentials.username" class="form-control" placeholder="{{'SIGNIN_FORM_EMAIL'| translate}}"/>
    <input type="password" ng-required name="password" ng-model="credentials.password" class="form-control" placeholder="{{'SIGNIN_FORM_PASSWORD'| translate}}"/>
    <button class="btn btn-lg btn-primary btn-block" type="submit">{{'SIGNIN' | translate}}</button>
    <div class="forgot-password">
        <a ui-sref="sendpasswordresetinformation">{{'SIGNIN_FORM_FORGOTTEN_PASSWORD' | translate}}</a>
    </div>
</form>

However, if the form has no credentials in the inputs (nothing as in not even the empty string) the form shakes only once.

Can someone please help?

edit 1:

  .factory('signinService', ['$http', function ($http) {
        return {
            signin: function (credentials, successCallback, errorCallback) {

                var transform = function (data) {
                    return $.param(data);
                }

                return $http.post('/api/signin', credentials, {
                    headers: {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'},
                    transformRequest: transform
                }).success(function (data, status, headers) {
                    if (status === 200) {
                        successCallback(status, headers('MEMBER_ROLE'));
                    }
                    else {
                        console.log('auth error');
                        successCallback('error');
                    }
                }).error(function(data, status, headers) {
                    console.log("error!", data, status, headers);
                    errorCallback();
                });
            }
        }
    }]);

Changing the controller to :

...
$scope.signin = function () {
            $scope.shake = false;
            $scope.$apply();//ADDED!
            ...

fixed the issue. Thanks floribon...

3
  • 1
    Jus to be sure, if your signinService is an ajax call no wrapped by angular ($htttp), you'll need to apply your scope after you set shakein order for the view to be updated: $scope.$apply() Commented Apr 14, 2015 at 17:13
  • @floribon: I have included signinService in edit 1. Commented Apr 14, 2015 at 18:10
  • can you please provide a fiddle? Commented Apr 14, 2015 at 18:11

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.