One Function does not recognize the other

Asked

Viewed 82 times

0

I’m having trouble setting up a service for Angularjs, I call a function and when it calls another says it does not recognize as a function what was called

Uncaught TypeError: teste.redirect is not a function

the code I’m using is this:

teste.service('testService', function () {
this.bla = function (a,b){
    console.log("ta na function do teste"+ a + " | " + b);
    teste.redirect("");
};


this.login = function(pass,user){
    var cPass = Sha256.hash(pass);
    var cUser = user;
    var json = {"user": cUser,"password": cPass};

    console.log(json)
    consumeService (null, JSON.stringify(json), "login", "POST", "alerta", function(result){
        var loginTO = result;
        if(loginTO != null){
            teste.redirect("logon.html");
        }
    }); 
};

this.redirect = function (destiny) {
    var url = "http://localhost:8080/web/";
    var direct = url + destiny;
    if($scope.validateToken()){ window.location(direct);
    }else{ window.location(url) ; }
};

Would anyone know where the mistake is?

2 answers

1


Create a variable to persist the scope reference of the original function:

    teste.service('testService', function () {

    var that = this;

    this.bla = function (a,b){
        console.log("ta na function do teste"+ a + " | " + b);
        that.redirect("");
    };


    this.login = function(pass,user){
        var cPass = Sha256.hash(pass);
        var cUser = user;
        var json = {"user": cUser,"password": cPass};

        console.log(json)
        consumeService (null, JSON.stringify(json), "login", "POST", "alerta", function(result){
            var loginTO = result;
            if(loginTO != null){
                that.redirect("logon.html");

        }
    }); 
};

this.redirect = function (destiny) {
    var url = "http://localhost:8080/web/";
    var direct = url + destiny;
    if($scope.validateToken()){ window.location(direct);
    }else{ window.location(url) ; }
};

1

I don’t know what your context is, but I’ll give you some advice. Do not burden angular services by exposing methods that will only be used locally. For this you can declare functions that will not be exposed for each instance of the service. See the example below:

teste.service('testService', function () {
    function metodoLocal(param) {
        console.log("Redirect..." + param);
    }

    return {
         metodoExposto: function(){
            metodoLocal("http://...");
         }
     };
});

Note the methods exposed and those that will be used internally to manage the service.

About solving your problem. You can create a variable as already mentioned, or you can prefix "this" to reference the instance methods.

teste.service('testService', function () {
    this.bla = function (a,b){
        teste.redirect("");
    };

    this.login = function(pass,user){
        this.bla("a", "b");
    };
});

Browser other questions tagged

You are not signed in. Login or sign up in order to post.