Mam usługa tak:angularjs usługa nie jest funkcją
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;
};
});
i kontroler tak:
app.controller('writeCtrl', function($scope, Utilities, students) {
$scope.students = students;
$scope.total_age = Utilities.sum($scope.students, 'age');
});
I zachować uzyskiwanie błąd
Typerror: Utilities.sum is not a function
Która jest mylące, ponieważ około tuzin innych funkcji w ramach usługi Utilities działa dobrze. Co powoduje problem i jak uruchomić funkcję?
Edit Aktualna wersja coffeescript
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')
Rozwiązanie:
funkcje coffeescript potrzebujemy powrotu:
App.service 'Utilities', ->
.....
return
Czy to jest prawidłowe wcięcie twojego kodu? CoffeeScript jest znaczący i wydaje się, że masz błąd wcięcia na linii 3 –
@DanPantry Hi Dan, wcięcie musiało zostać popsute, kiedy wkleiłem to na stackoverflow, ale tak, wcięcie w moim rzeczywistym kodzie jest poprawne. –