Abdul Ahmad
Abdul Ahmad

Reputation: 10021

passing functions into angular instead of 'function()'

I've been working with angular a lot lately and one thing I wanted to do was use a declared function instead of a place that takes the word "function() {}", like in a promise callback for example. I had a few server requests that, on error, all do the same thing, so instead of saying:

.then(function(data) {

}, function(error) {

});

I wanted to do the following:

.then(function(data) {
}, handleError(error));

function handleError(error) { console.log(error); }

but it wasnt working. Did I make a mistake in how I tried to do this or is this just not possible?

Upvotes: 0

Views: 31

Answers (1)

Bergi
Bergi

Reputation: 664648

When writing handleError(error), you're calling the function (with some error variable as the argument, which probably doesn't even exist). You just want to pass the function:

.then(function(data) {
     …
}, handleError);

No parenthesis!

Upvotes: 3

Related Questions