Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript AngularJS Basics (1.x) Services in Angular Create an Angular service.

jason chan
jason chan
31,009 Points

Create a method named testingMyService on the service that logs the string, "This is my service!" when called. E.g.: myS

stuck

app.js
angular.module('foobar', [])
.service('myService', function testingMyService (service){
 this.service = function () {
 console.log("This is my service!");
 },
 myService.testingMyService()
})
;

3 Answers

jason chan
jason chan
31,009 Points
angular.module('foobar', [])
.service('myService', function myService(){
  this.testingMyService = function(){
  console.log("This is my service!")
  };
  });

SOLVED

Justin Lovato
Justin Lovato
1,629 Points

I had the exact same code but had to use "myService" with double quotes instead of 'myService' in single quotes. Hopes this helps somebody.

Abdulrahman D
Abdulrahman D
16,420 Points
angular.module('foobar', [])
.service('myService', function(){
  this.testingMyService = function(){
  console.log("This is my service!")
  };
  });

The callback function should be anonymous.

Samuel Gates
Samuel Gates
1,181 Points

of course, you don't need to define your function as myService for it to work.