Markus Zuberhauser
Markus Zuberhauser

Reputation: 67

How can i add more then one functions in a service or factory?

i must write a Service who has a lot of functions inside. This i must inject into a controller. But, then i write a factory with 3 or more functions, angular found the first one, all others are undefined. - Why?

mySystem.factory('testFactory', function($http) {
return {
checkDates: function() {
    myData.VDate = myData.VDate.format('dd.MM.yyyy');
}

return {
    checkrequiered: function() {
    var check = true;
    if (myData.locId.lenght === 0) {
        check=false;
    }
    return check;
}

return {
    updateData: function() {
       '...'
    }
});

Whats wrong?

Upvotes: 0

Views: 578

Answers (1)

JLRishe
JLRishe

Reputation: 101662

What's wrong is that you have three return statements, which means all but the first will be ignored. Return all the functions in a single object:

return {
    checkDates: function() {
        myData.VDate = myData.VDate.format('dd.MM.yyyy');
    },
    checkRequired: function() {
        return (myData.locId.length !== 0);
    },
    updateData: function() {
       '...'
    }
};

Upvotes: 1

Related Questions