简体   繁体   中英

Angularjs factory catch function not applying scope

I upgraded my Angular from version 1.3.14 to 1.4.5

When the below code goes into the then function of the loginService factory it apply the values to the scope. It does not apply to the scope when going into the catch/error function. Does it work different in 1.4.5?

    (function () {
        'use strict';
        var loginController = angular.module('test.login.controller', ['ngMessages', 'ui.bootstrap']);

        loginController.controller('LoginController', function (securityService, $state, $scope, loginService, $log, $http, EXT_API) {

            securityService.clearCredentials();

            var loginCtrl = this;
            loginCtrl.credentials = {};
            loginCtrl.loginButton = 'Submit';
            loginCtrl.cancelButton = 'Cancel';
            loginCtrl.heading = 'Test Engine';
            loginCtrl.usernameLabel = 'User Name';
            loginCtrl.passwordLabel = 'Password';
            loginCtrl.showAlertMessage = false;
            loginCtrl.alertMessage = '';

            loginCtrl.login = getLogin;

            function getLogin(credentials) {
                $http.post(EXT_API + '/rest/login/authenticate/123456789', JSON.stringify({credentials: credentials}))
                        .then(function (response) {
                            $log.info('Login successful');
                            loginCtrl.showAlertMessage = true;
                            loginCtrl.alertMessage = 'Valid User Name and Password';

                        }, function (result) {
                            $log.info('Login Failed');
                            loginCtrl.showAlertMessage = true;
                            loginCtrl.alertMessage = 'Invalid User Name or Password';
                        });
            };

        });
    })();

On the login page the form is setup as follows when submit is clicked.

<form class="form-horizontal" name="loginForm" ng-submit="loginCtrl.login(loginCtrl.credentials)" novalidate>
        <input type="text" class="form-control" id="username" name="username" placeholder="User Name" ng-model="loginCtrl.credentials.username" required>
        <input type="password" class="form-control" id="password" name="password" placeholder="Password" ng-model="loginCtrl.credentials.password" required>
</form>

After allot of searching in the application I found a interceptor that handles certain HTTP error codes.

After removing the interceptor everything works perfectly. Some refactoring is needed to make the page work.

Thanks

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM