Shivendra
Shivendra

Reputation: 3

Can't access function in Angular.js service

I am trying to create and use a new service in AngularJS, however, I get the following error -

Error message ProjectService.one is not a function

The Javascript -

var app = angular.module('app', ['ngRoute']);

app.service('ProjectService', function () {   
    this.one = function one() {
        console.log('test service');
    };
});

app.controller('ProjectsController', ['$scope', function (ProjectService, Test) {
    ProjectService.one();
}]);

Upvotes: 0

Views: 562

Answers (2)

M.S.
M.S.

Reputation: 4433

You've to inject the ProjectService and other required dependent modules as mentioned below:

app.controller('ProjectsController', ['$scope','ProjectService','Test', function ($scope,ProjectService, Test) {
  ProjectService.one();
}]);

Upvotes: 0

Frode
Frode

Reputation: 3455

There is something wrong in your controller declaration. Your ProjectService parameter matches the $scope service. Do this instead;

app.controller('ProjectsController', ['$scope', 'ProjectService', 'Test', function ($scope, ProjectService, Test) {
    ProjectService.one();
}]);

The service-parameters must match the array of services (same number and order)

Upvotes: 3

Related Questions