2016-04-10 12 views
0

gefunden Ich versuche, einen Dienst in einen Controller zu injizieren, aber der Dienst nicht gefunden Fehler:AngularJS Service nicht

angular.js:13424 Error: [$injector:unpr] http://errors.angularjs.org/1.5.3/$injector/unpr?p0=<section data-ui-view="" class="view-container {{main.pageTransition.class}} ng-scope" data-ng-animate="1">copeProvider%20%3C-%20%24scope%20%3C-%20authenticationService 
    at Error (native) 
    at http://localhost:3000/bower_components/angular/angular.min.js:6:416 
    at http://localhost:3000/bower_components/angular/angular.min.js:43:7 
    at Object.d [as get] (http://localhost:3000/bower_components/angular/angular.min.js:40:270) 
    at http://localhost:3000/bower_components/angular/angular.min.js:43:69 
    at d (http://localhost:3000/bower_components/angular/angular.min.js:40:270) 
    at e (http://localhost:3000/bower_components/angular/angular.min.js:41:1) 
    at Object.instantiate (http://localhost:3000/bower_components/angular/angular.min.js:41:364) 
    at Object.<anonymous> (http://localhost:3000/bower_components/angular/angular.min.js:42:184) 
    at Object.invoke (http://localhost:3000/bower_components/angular/angular.min.js:41:295) 

Mein Controller:

(function() { 
    'use strict'; 

    angular.module('app.page') 
    .controller('authCtrl', ['$scope', '$window', '$location','authenticationService', authCtrl]); 


    function authCtrl($scope, $window, $location, authenticationService) { 
     $scope.login = function() { 
      authenticationService.test(); 
     } 

     $scope.signup = function() { 
      $location.url('/') 
     } 

     $scope.reset = function() { 
      $location.url('/') 
     } 

     $scope.unlock = function() { 
      $location.url('/') 
     } 
    } 
})(); 

Service:

(function() { 
    'use strict'; 

    angular.module('app.page') 
     .service('authenticationService', ['$scope', '$window', authenticationService]); 

    function authenticationService() { 
     this.test = function() 
     { 
      alert('test'); 
     } 
    } 
})(); 

Was mache ich hier falsch?

+1

Warum Sie '$ scope' in' authenticationService', und was ist zu Abhängigkeitsliste in der Funktion des Service injizieren ? Sollte nicht '[$ scope', '$ window', authenticationService] 'zu' [authenticationService] '' geändert werden? –

+0

Deklarieren Sie authCtrl vor dem Aufruf von angular.module.controller. authCtrl sollte vor der Funktionsdeklaration undefiniert sein. –

+0

Danke für Ihre Hilfe. – Jamie

Antwort

2

Die Anzahl der Injektionen im Serviceaufruf ['$scope', '$window', authenticationService] stimmt nicht mit der Argumentliste Ihrer Servicefunktion () überein.

Um Fehler wie dies zu vermeiden, schlage ich vor, Sie $ zu verwenden injizieren (https://docs.angularjs.org/guide/di#-inject-property-annotation):

(function() { 
    'use strict'; 

    angular.module('app.page').service('authenticationService', authenticationService); 

    authenticationService.$inject = []; 

    function authenticationService() { 
     this.test = function() 
     { 
      alert('test'); 
     } 
    } 
})(); 
Verwandte Themen