2

I'm stuck with an issue when try to implement registering function for a web.

Honestly, i just read some document about angularJs (and little bit about javascript) yesterday + under push. So, do not so hard to me, please :)

Below is my directive:

 var app = angular.module('register-user', []);

    app.directive('register', function () {
        return {
            restrict: 'E',
            templateUrl: 'app/views/register.html',
            controller: RegistestCtrl,
            controllerAs: 'registerCtrl'
        };
    });

The controller:

RegistestCtrl.$inject = ['RegisterService'];
function RegistestCtrl(registerService) {

    this.user = {};
    this.isProcessing = false;

    this.isRegistering = function () {
        return this.isProcessing;
    };

    this.setRegistering = function (value) {
        this.isProcessing = value;
    };

    this.register = function () {
        this.setRegistering(true);
        function registerSuccess(result) {
            alert("registerSuccess: " + JSON.stringify(result));
            this.setRegistering(false);
        }

        function registerFail(error) {
            alert(error.message)
            this.setRegistering(false);
        }
        registerService.register(this.user.email, this.user.pass).then(registerSuccess, registerFail);
    };
}

And here is the service in case:

angular.module('app').factory('RegisterService', RegisterService);
RegisterService.$inject = ['$http', 'ENDPOINT'];

function RegisterService($http, endpoint) {

    var service = {};
    service.register = register;
    return service;

    function register(email, pass) {
        return $http({
            method: 'POST',
            url: endpoint.URL + endpoint.REGISTER,
            data: {
                "email": email,
                "password": pass
            }
        }).then(handleSuccess, handleError);
    }

    function handleSuccess(res) {
        return res.data;
    }

    function handleError(error) {
        return function () {
            return {
                success: false,
                message: error
            };
        };
    }
}

Finally, the issues is:

TypeError: this.setRegistering is not a function at registerSuccess

It points to these lines:

this.setRegistering(false);
this.setRegistering(false);

Other thing is Ok, but i want to call this to hide the progress circle when finish register. Could you give me hint here.

I think something went wrong here with the scope of variable. But i don't know where to start.

Tomasz Jakub Rup
  • 10,502
  • 7
  • 48
  • 49
Lạng Hoàng
  • 1,790
  • 3
  • 17
  • 32
  • 6
    See [How to access the correct `this` inside a callback?](http://stackoverflow.com/questions/20279484/how-to-access-the-correct-this-context-inside-a-callback). – fracz Dec 18 '15 at 07:59
  • 1
    All you need to do is to remove all 'this' from your callback functions, and as @fracz shared you can use another variable which refer this eg : var self=this; and use self for callbacks. because 'this' refers to callback.. – Anupam Singh Dec 18 '15 at 10:42
  • @fracz: nice link! thanks you all. – Lạng Hoàng Dec 27 '15 at 01:54

0 Answers0