55366

angular Uncaught ReferenceError: Service is not defined

Question:

I have the following component in which I am trying to inject a service:

angular. module('phoneList'). component('phoneList', { templateUrl: '/static/common/angular/phone-list/phone-list.template.html', controller: ['$http', 'authenticationService', function PhoneListController($http, authenticationService) { var self = this; authenticationService.authenticate().then(function(){ console.log('it worked!!!!!!!!'); }); } ] });

The service looks like this:

angular.module('authentication').factory('authenticationService', function($http, $se){ function authenticate(){ $http.post('/o/token/', data, config) .success(function (data, status, headers, config) { console.log('auth service: '+data['access_token']); $sessionStorage.access_token = data['access_token']; }); } function getToken(){ return $sessionStorage.access_token; } return { authenticate:authenticate, getToken:getToken }; });

My phone-list.module.js looks like this:

angular.module('phonecatApp', [ 'phoneList', 'authentication', ]); angular.module('phoneList', ['authentication']);

When I run this i get the error:

<blockquote>

Uncaught ReferenceError: authenticationService is not defined

</blockquote>

When I put 'authenticationService' in '', I get the error:

<blockquote>

Error [$injector:unpr] authtenticationService

</blockquote>

Answer1:

It seems the service isn't properly injected into the PhoneListController.

Change it to:

controller: ['$http', 'authenticationService', function PhoneListController($http, authenticationService) { ...

The strings in the array are just to keep the injected dependency references minification safe. The service still needs to be added as a function argument.

Also be sure to call angular.module <em>once</em> for each component:

app.module.js

angular.module('phonecatApp', [ 'ngRoute', 'phoneList', 'authentication', ]);

phone-list.module.js

angular.module('phoneList', ['authentication']);

Recommend

  • ASP.NET windows authentication should always ask for credentials
  • Laravel 4 routing not working due to .htaccess file?
  • Authentication failed with Azure Active Directory in Windows Phone
  • Suppressing passwd when calling sqlplus from shell script
  • Implicit joins and Where in Doctrine - how?
  • Authentication in Play! and RestEasy
  • Button text different than value submitted in query string
  • D3 get axis values on zoom event
  • Detect when Facebook like button is clicked
  • Needing to do .toArray() to get output of mongodb .find() on key name not value
  • Abort upload large uploads after reading headers
  • Make VS2015 use angular-cli ng at build time in a .NET project
  • Allowing both email and username for authentication
  • jQuery .attr() and value
  • Email format validation in mvc3 view
  • Regex thinks I'm nesting, but I'm not
  • req.body is undefined - nodejs
  • Apache 2.4 and php-fpm does not trigger apache http basic auth for php pages
  • How to get a value (ex: baseURL) in every Karate feature?
  • How to recover from a Spring Social ExpiredAuthorizationException
  • angularjs unit test when to use $rootScope.$new()
  • How to set/get protobuf's extension field in Go?
  • Submit form in a displaytag pagination
  • script to move all files from one location to another location
  • Perl system calls when running as another user using sudo
  • Can I have the cursor start on a particular column by default in jqgrid's edit mode?
  • Can a Chrome extension content script make an jQuery AJAX request for an html file that is itself a
  • Upload files with Ajax and Jquery
  • How to pass list parameters for each object using Spring MVC?
  • How to delete a row from a dynamic generate table using jquery?
  • AngularJs get employee from factory
  • log4net write single file for each call to log.info
  • Proper way to use connect-multiparty with express.js?
  • using HTMLImports.whenReady not working in chrome
  • Exception on Android 4.0 `android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode)`
  • Authorize attributes not working in MVC 4
  • EntityFramework adding new object to nested object collection
  • Net Present Value in Excel for Grouped Recurring CF
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize
  • How to load view controller without button in storyboard?