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 trialcb123
Courses Plus Student 9,858 PointsHow 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
32,285 PointsYour 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
8,799 Pointsangular.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();