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

How to create an Angular Service Method with Callback Log

I am trying to finish up the workshop quiz in Angular. Step 1 create service. Step 2 Create Callback. Step 3 below I am having trouble with completing.

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

angular.module('foobar', []) .service('myService',function(callback) { console.log("This is my Service") } );

What am I doing wrong?

2 Answers

Van Doan
Van Doan
32,285 Points

Your service bit doesn't look quite right. It should look something like this. Notice the "this.testingMyService" section. Cheers.

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

Balázs Buri
Balázs Buri
8,799 Points
angular.module('foobar', []) .service('myService',function(callback) { 
    this.testingMyService = function(){
        console.log("This is my Service");
    }
 } );

So inside your service "myService" you define a new function and add it to "this" which is "myService" your new function is called testingMyService. When you use your service "myService" you can call testingMyService inside it: myService.testingMyService();