简体   繁体   中英

AngularJS - Toaster not working in Controller

Service.js

this.userLogin = function (username, password) {

        var dataBody = $.param({'username': username,'password': password});
        return $http({
            method: 'POST',
            url: servicePathURL,
            data: dataBody,
            headers: {
                "Authorization": "Basic",
                "Content-Type": "application/x-www-form-urlencoded"
            }
        })

        .then(function (response) {
            $rootScope.globals = {
                currentUser: {
                    username: username,
                }
            };
            return response;

        }).catch(function (error) {
            throw error;
        });
    };

Controller.js

AuthenticationServiceLogin.userLogin($scope.username, $scope.password)

            .then(function (response) {

                if (response.status ==200) {   
                    toaster.pop('success', "", "Login Successful");
                    $location.path('/home');
                }

            }).catch(function (error) {
                toaster.pop('error', "", error.statusText);
        });
  1. In Controller.js, toaster.pop('error', "", error.statusText); is not being called when there is an exception while user logs in.
  2. Also I have used $http method, is there any advantage to returning a $q.defer() promise rather than an $http promise or considered as best practice ? If yes, how can I modify above $http code into promise ?

Your code appears to be fine. So long as you re-throw any errors encountered by your $http call, they should propagate all the way up to the controller. I'm inclined to say that any problems with your error handling are not in the code that you've posted.

There's no advantage to having a catch handler in service.js if you're not going to do any work with the error thrown. Service.js will want to look like this:

Service.js

this.userLogin = function (username, password) {
    return $http({
        method: 'POST',
        url: servicePathURL,
        data: $.param({'username': username,'password': password}),
        headers: {
            "Authorization": "Basic",
            "Content-Type": "application/x-www-form-urlencoded"
        }
    })
    .then(function (response) {
        $rootScope.globals = {
            currentUser: {
                username: username,
            }
        };
        return response;
    // The catch block here is useless. The promise returned by $http will transmit any
    // errors on its own.        
    //}).catch(function (error) { 
    //    throw error;
    });
};

In response to your second question: there is no advantage to using $q.defer() instead of returning the promise returned by $http itself, and a number of major disadvantages - namely that any exceptions thrown by your login method will disappear. See The Deferred Anti-pattern here for details: https://github.com/petkaantonov/bluebird/wiki/Promise-anti-patterns

$http is definitely the preferred option.

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