AngularJS - Toaster not working in Controller

Slimshadddyyy

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 ?
s3raph86

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Fire notification toaster in any controller angularjs

From Dev

AngularJS Controller not working in nested Controller

From Dev

AngularJS greetController Controller not working

From Dev

angularjs 1.3 controller as not working

From Dev

multiple controller not working in angularjs

From Dev

AngularJS controller not working

From Dev

angularjs 1.3 controller as not working

From Dev

AngularJS greetController Controller not working

From Dev

AngularJS Toaster does not show on callback

From Dev

AngularJS Toaster does not show on callback

From Dev

Changing toaster class from the controller toaster.pop()

From Dev

Angularjs - data binding not working with controller

From Dev

AngularJS ng-controller not working

From Dev

Angularjs $setPristine not working with controller as syntax

From Dev

Show and Hide Controller not working, AngularJS

From Dev

Simple AngularJS Controller Demo Not Working

From Dev

AngularJS ng-click not working with configured Controller

From Dev

AngularJS beginner: ng-controller not working

From Dev

AngularJS ng-controller not working after bootstraping

From Dev

JS function defined in AngularJS controller not working

From Dev

AngularJS stops working as soon as I include a controller

From Dev

Angularjs controller not working, throws error (newbie)

From Dev

Cannot make controller tests working with AngularJS + RequireJS

From Dev

AngularJS controller with application don't working

From Dev

AngularJS controller.. Why the code id not working?

From Dev

AngularJS function in controller and factory not working when separated

From Dev

Angular : How can i define toaster in factory and call it in Controller

From Dev

Angularjs-toaster always displays toast notification on top-right

From Dev

angularjs toaster failing to change from sticky to dismiss dynamically

Related Related

  1. 1

    Fire notification toaster in any controller angularjs

  2. 2

    AngularJS Controller not working in nested Controller

  3. 3

    AngularJS greetController Controller not working

  4. 4

    angularjs 1.3 controller as not working

  5. 5

    multiple controller not working in angularjs

  6. 6

    AngularJS controller not working

  7. 7

    angularjs 1.3 controller as not working

  8. 8

    AngularJS greetController Controller not working

  9. 9

    AngularJS Toaster does not show on callback

  10. 10

    AngularJS Toaster does not show on callback

  11. 11

    Changing toaster class from the controller toaster.pop()

  12. 12

    Angularjs - data binding not working with controller

  13. 13

    AngularJS ng-controller not working

  14. 14

    Angularjs $setPristine not working with controller as syntax

  15. 15

    Show and Hide Controller not working, AngularJS

  16. 16

    Simple AngularJS Controller Demo Not Working

  17. 17

    AngularJS ng-click not working with configured Controller

  18. 18

    AngularJS beginner: ng-controller not working

  19. 19

    AngularJS ng-controller not working after bootstraping

  20. 20

    JS function defined in AngularJS controller not working

  21. 21

    AngularJS stops working as soon as I include a controller

  22. 22

    Angularjs controller not working, throws error (newbie)

  23. 23

    Cannot make controller tests working with AngularJS + RequireJS

  24. 24

    AngularJS controller with application don't working

  25. 25

    AngularJS controller.. Why the code id not working?

  26. 26

    AngularJS function in controller and factory not working when separated

  27. 27

    Angular : How can i define toaster in factory and call it in Controller

  28. 28

    Angularjs-toaster always displays toast notification on top-right

  29. 29

    angularjs toaster failing to change from sticky to dismiss dynamically

HotTag

Archive