Search code examples
javascriptangularjsangularjs-scope

angularJs, $rootScope.$emit() returns an object of data


I am trying to expose a response from the service to the controller via $rootScope.$emit('name', response.data), but it doesn't work.

My service:

/**API Post for user login*/
        function login(username,password){
            return $http.post('/api/login/',{
                   username:username,
                   password:password
            }).then(loginSuccessFn, loginErrorFn);

            function loginSuccessFn(response, status, headers, config){
                Authentication.setAuthenticatedAccount(response.data);

                window.location = '/';
            }
            function loginErrorFn(response, status, headers,config){
                $rootScope.$emit('errorLogin', response.data);
            }

          }

My Controller:

//Catching the Authentication.login errorFNn response.data
        $rootScope.$on('errorLogin', function(p){
            vm.isSuccess = p;
        })

and this is what happens in the view:

{"name":"errorLogin","targetScope":"$SCOPE","defaultPrevented":false,"currentScope":null}

can someone help me out


Solution

  • Change:

    $rootScope.$on('errorLogin', function(p){
        vm.isSuccess = p;
    });
    

    To:

    $rootScope.$on('errorLogin', function(e, p){
        vm.isSuccess = p;
    });