0

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();
}]);

3
  • 2
    try this.one = function () {....} Commented May 12, 2016 at 10:00
  • 1
    that shouldn't matter Commented May 12, 2016 at 10:01
  • @Bikee Tried anonymous function, but that doesn't work either. Commented May 12, 2016 at 10:02

2 Answers 2

3

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)

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for that. Can't believe I messed that up. @M.S Cheers
0

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();
}]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.