angularjs service is not a function
10,597
Service never returns an object, basically it binds a method or variable to its context; nothing but this
& then it returns a new object
which contains all the things which were binded to this
.
Code
app.service('Utilities', function() {
this.sum = function(items, prop) {
var count, total;
total = 0;
count = 0;
if (items === null) {
total = 0;
}
while (count < items.length) {
total += items[count][prop] * 1 || 0;
count++;
}
return total;
};
//
// ..other utility method should lies here..
//..do your stuff
});
Update
You should change your coffee script service from
app.service 'Utilities' ->
to
app.service 'Utilities' () ->
Comments
-
Ryan.lay almost 2 years
I have a service like so:
app.service('Utilities', function() { this.sum = function(items, prop) { var count, total; total = 0; count = 0; if (items === null) { total = 0; } while (count < items.length) { total += items[count][prop] * 1 || 0; count++; } return total; }; });
and a controller like so:
app.controller('writeCtrl', function($scope, Utilities, students) { $scope.students = students; $scope.total_age = Utilities.sum($scope.students, 'age'); });
And I keep getting the error
Typerror: Utilities.sum is not a function
Which is confusing because about a dozen other functions under the Utilities service is working fine. What's causing the issue, and how do I get the function to work?
Edit Actual Coffeescript version
app.service 'Utilities', -> @sum = (items, prop) -> total = 0 count = 0 if items == null total = 0 while count < items.length total += (items[count][prop]*1 || 0) count++ return total app.controller 'writeCtrl', ($scope, Utilities, students) -> $scope.students = students $scope.total_age = Utilities.sum($scope.students, 'age')
Solution:
Coffeescript functions need a return:
App.service 'Utilities', -> ..... return
-
Ryan.lay almost 9 yearssorry, it's a typo from the js2coffee app. I'm using @sum = (items, prop) -> with coffeescript.
-
Dan almost 9 years@Ryan.lay paste your actual code instead of a transpiled version.
-
Pankaj Parkar almost 9 years@Ryan.lay could you update you code then..what you have in your code
-
Pankaj Parkar almost 9 years@Ryan.lay it doesn't seems like you have updated your question..Could you please do update it with proper code..
-
Dan almost 9 years@PankajParkar give him time. He might be busy. You've only given him 10 minutes to update his code. Most of my meetings in work last more than 10 minutes.
-
Pankaj Parkar almost 9 years@DanPantry np dude.. he could take his own time.. :)
-
Ryan.lay almost 9 years@PankajParkar Thanks for your patience, I've updated my code.
-
Pankaj Parkar almost 9 years@Ryan.lay I think you should try change
app.service 'Utilities' ()->
instead ofapp.service 'Utilities' ->
-
Pankaj Parkar almost 9 years@Ryan.lay as you said in your question
dozen other functions under the Utilities service is working fine
where are you are written this other functions.. -
Ryan.lay almost 9 yearsARRGHHH rookie mistake. I was missing the return at the end of the Utilities service. The return was accidentally overwritten when I added the sum function. Thank you so much for your time and effort Pankaj and @DanPantry.
-
Dan almost 9 yearsChanging the
() ->
is a superficial change and will not fix the issue.->
and()->
evaluate the same. -
Pankaj Parkar almost 9 yearsGlad to help you..Thanks..@DanPantry why you don't feel question should shouldn't havr angularjs-service tag?
-
Dan almost 9 years@PankajParkar I've posted my thoughts about that on the meta, when I first looked at the tag I didn't see it had so many questions attached to it and it gave me the impression you were creating a new tag. So I apologize for removing it, that was inappropriate of me.