简体   繁体   English

AngularJS-烤面包机不能在控制器中工作

[英]AngularJS - Toaster not working in Controller

Service.js 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 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); 在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 ? 我还使用了$http方法,返回$q.defer()承诺而不是$http承诺或被认为是最佳实践有什么好处吗? If yes, how can I modify above $http code into promise ? 如果是,我该如何将以上$http代码修改为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. 只要您重新抛出$http调用遇到的任何错误,它们就应该一直传播到控制器。 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中使用catch处理程序没有任何优势。 Service.js will want to look like this: Service.js将看起来像这样:

Service.js 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. 回答您的第二个问题:使用$q.defer()而不返回$http本身返回的承诺没有任何好处,还有许多主要缺点-即您的登录方法抛出的任何异常都将消失。 See The Deferred Anti-pattern here for details: https://github.com/petkaantonov/bluebird/wiki/Promise-anti-patterns 有关详细信息,请参见此处的延迟反模式: https : //github.com/petkaantonov/bluebird/wiki/Promise-anti-patterns

$http is definitely the preferred option. $http绝对是首选。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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