Ich habe Probleme beim Versuch, meine Factory/Service-Funktion von meinem Controller aus zu aktivieren. Ich habe viele Threads dazu gesehen und habe verschiedene Lösungen ausprobiert, aber es bringt mich dazu zu glauben, dass ich etwas Einfaches vermisse. Hier ist der Code (Fehlermeldung versehen unten):AngularJS Factory.XXX ist keine Funktion
'use strict';
var smacApp = angular.module('smacApp', ['ngRoute']);
smacApp.config(function($routeProvider) {
$routeProvider
.when("/login", {
templateUrl: "templates/login.html",
controller: "LoginController"
})
});
smacApp.factory('AuthenticationService', function() {
var users = ["Bob", "Joe"];
return {
all: function() {
return users;
},
first: function() {
return users[0];
}
};
});
smacApp.controller('LoginController', function($scope, AuthenticationService) {
$scope.users = AuthenticationService.all();
console.log($scope.users);
});
smacApp.run.$inject = ['$rootScope', '$routeParams'];
smacApp.run(function($rootScope, $routeParams) {
});
Die Fehlermeldung Ich erhalte ist:
angular.js:9778TypeError: AuthenticationService.all is not a function
at new <anonymous> (http://localhost/smac3/app.js:61:39)
at d (http://localhost/smac3/lib/angular/js/angular.min.js:34:265)
at Object.instantiate (http://localhost/smac3/lib/angular/js/angular.min.js:34:394)
at http://localhost/smac3/lib/angular/js/angular.min.js:66:112
at link (http://localhost/smac3/lib/angular/js/angular- route.js:913:26)
at J (http://localhost/smac3/lib/angular/js/angular.min.js:53:345)
at f (http://localhost/smac3/lib/angular/js/angular.min.js:46:399)
at http://localhost/smac3/lib/angular/js/angular.min.js:46:67
at http://localhost/smac3/lib/angular/js/angular.min.js:47:303
at u (http://localhost/smac3/lib/angular/js/angular.min.js:51:28) <div ng-view="" id="container" class="ng-scope">
Jede Hilfe ist sehr zu schätzen!
manchmal passiert es, wenn Sie eine Variable und eine Funktion haben, mit der selbe Name. wie Variable 'all' und eine Funktion 'all'. Nun, ich sehe nicht alle in Ihrem Code verfügbar, aber ich habe nur eine Möglichkeit gesagt. AuthenticationService.first() funktioniert? –
Danke Sumeet! Leider keine Freude ... TypeError: AuthenticationService.first ist keine Funktion –
es ist seltsam, weil ich die exakt gleiche Syntax verwenden und ich sehe keinen Fehler lol: p denke –