Lucas Müller
Lucas Müller

Reputation: 392

Doing a function variable on service AngularJS

i'm building a login service to my website and i need help on this: i want to check if login was done and if is true do the function at 'yes' variable. Else, do the 'no' variable. Like this:

app.service('login', function($cookies) {
// Cookie status = login status
var cookie_status = $cookies.get('login_status');

// Cookie token = login token
var cookie_token = $cookies.get('login_token');

this.status = function(){
    return cookie_status
};
this.iflogin = function(yes, no){
    if(cookie_status=='yes'){
        return yes;
    } else {
        no;
    }
}; });

But, therefore, when i call this function the function at 'yes' or 'no' variable aren't done. That is my call:

login.iflogin(function yes(){
    $scope.login = "Conta";
}, function no(){
    $scope.login = "Login";
});

Where is the problem!?

Thank you and sincerely, Lucas.

Upvotes: 2

Views: 25

Answers (1)

Michele Ricciardi
Michele Ricciardi

Reputation: 2117

You don't need to return inside the iflogin function, you simply need to call either of the functions like this:

this.iflogin = function(yes, no){
    if(cookie_status=='yes'){
        yes();
    } else {
        no();
    }
};

Upvotes: 1

Related Questions