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 Services and Dependencies Services: Factory

Thomas Wreiner
Thomas Wreiner
6,954 Points

Not I'm not understanding here? Tried setting course.title but nothing works.

Tried setting course.title = "Angular... " as well, doesnt work.

app.js
angular.module('treehouseCourse', [])
  // YOUR CODE HERE
.factory('Course', function() {
  var course = {title:"Intro to Angular"};
  return course.title;
});
index.html
<!DOCTYPE html>
<html ng-app="treehouseCourse">
  <head>
    <title>Angular.js</title>
    <script src="js/angular.js"></script>
    <script src="app.js"></script>
  </head>
  <body ng-controller="MyCourseCtrl">
  </body>
</html>

2 Answers

Colin Bell
Colin Bell
29,679 Points

Your original answer is returning just "Intro to Angular".

What needs to be returned is the entire object, that is: {title : "Intro to Angular"}

your code would work as such:

app.js
angular.module('treehouseCourse', [])
  // YOUR CODE HERE
.factory('Course', function() {
  var course = {title:"Intro to Angular"};
  return course;
});

Or you just exclude the use of a variable, and write it like this:

app.js
angular.module('treehouseCourse', [])
.factory ('Course', function() {
  return {
    title : 'Intro to Angular'
  }
})

A little context might help this thread be more useful.

Here's a link to the challenge:

http://teamtreehouse.com/library/angularjs-2/services-and-dependencies/services-factory

Challenge Task 1 of 2

We've started a new module called 'treehouseCourse' for you. Write a definition for a service that is part of this module. It should be called 'Course'. Don't worry about making it do anything else just yet.

Challenge Task 2 of 2

Update the 'Course' factory's function to return an object with a single property, 'title', with a value of 'Intro to Angular'.

The key to understanding what they want is that when they talk about writing a "definition for a service"

What they actually mean you is used are supposed to use '.factory' (not the word 'service').

How would you know this?

I have really no idea.

I guess it's a quirk in AngularJS

(but it would have been really really helpful if the Treehouse person who wrote this question gave us a hint...)

So besides watching the videos, I wasted several hours of my life digging around in the Angular docs

and (eventually) found this services page: https://docs.angularjs.org/guide/services

It says (quoting the part I eventually realized was the most relevant):

Creating Services

Application developers are free to define their own services by registering the service's name and service factory function, with an Angular module.

..then in the "Registering services" section it actually has some code that uses '.factory',

...Unfortunately not in any way that passes this code challenge, though.

However, discovering that secret key piece of info eventually let me to this forum thread.

Colin's second piece of code pass both challenges. :thumbsup:

Note: I'm really getting addicted to using emoji now that I finally found the Emoji Cheat Sheet page

with all the MarkDown emoticons you can use:

http://www.emoji-cheat-sheet.com/