One Function does not recognize the other

0

I'm having trouble setting up a service for angular JS, I call a function and when it calls another it says that it does not recognize what it called as a function

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 error is?

    
asked by anonymous 16.10.2015 / 16:24

2 answers

1

Create a variable to persist the original function's scope reference:

    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) ; }
};
    
16.10.2015 / 16:42
1

I do not know what your context is, but I'll give you some advice. Do not overload angular services by exposing methods that will be used only 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://...");
         }
     };
});

Notice 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 instance methods.

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

    this.login = function(pass,user){
        this.bla("a", "b");
    };
});
    
16.10.2015 / 17:04