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

Travis Thompson
Travis Thompson
12,976 Points

Add controller called 'MyCourseCtrl' to the module... STUMPED

I simply cannot figure out the code challenge that states "Add controller called 'MyCourseCtrl' to the module, which requires the factory you just defined using array notation. Within the controller, log the injected factory to the console using console.log."

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

angular.module('treehouseCourse')
.controller('MyCourseCtrl', Course[
  // YOUR CODE HERE
  console.log(this.title);
)];

Any insight into this task would be super helpful. Thank You!

5 Answers

Chris Shaw
Chris Shaw
26,676 Points

Hi Travis,

You have a few issues with your code one of which Marcus Parsons mentioned in his post which is your closing square bracket is on the wrong side of the parenthesis, the other issues are as follows:

  1. You don't have Course set as a string within the array notation as shown in the previous video
  2. you haven't declared a function which is required for your code to execute within the controller

What you should end up with is the following.

angular.module('treehouseCourse')
    .controller('MyCourseCtrl', ['Course', function(Course) {
        // YOUR CODE HERE
    }]);

The better answer and I say better because the AngularJS docs say it non-stop; is to have $scope declared as well since controllers require it to pass data to the DOM.

angular.module('treehouseCourse')
    .controller('MyCourseCtrl', ['$scope', 'Course', function($scope, Course) {
        // YOUR CODE HERE
    }]);

Happy coding!

Good looking out, Chris! I haven't even yet taken any AngularJS courses, but this will help me too when I do.

Travis Thompson
Travis Thompson
12,976 Points

Hello Marcus and Chris,

Thank you for your input, I was able to figure this out with your help.

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

angular.module('treehouseCourse')
  .controller('MyCourseCtrl' ,['$scope', 'Course', function($scope, Course){
    console.log(Course);
  }]);

Hey Travis Thompson,

It looks as though the ending ) and ] in your .controller module are in the wrong place. The ] should be first to close since it was last to be opened. Try switching it like so:

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

angular.module('treehouseCourse')
.controller('MyCourseCtrl', Course[
  // YOUR CODE HERE
  console.log(this.title);
//Switched place of ] and ) 
]);

Note: The code provided by neither Chris, nor Marcus, was helpful (or passed the challenge).

But Travis got it right with his final posted code. (Thanks)

Chris Shaw
Chris Shaw
26,676 Points

Hi James,

The reason Travis' code worked is because my examples is only an shell meaning you need to provide the code where it says still in order to pass the challenge.

I feel as though you've simply tried to copy and paste this code without understanding it as prior to posting my answer I did test it against the challenge with all the required code and it did pass.

I recommend you go back over my answer as I explain the solutions to the. problems originally faced rather than giving the entire solution to the challenge.

Hope that clears things up.

maratfaizov
maratfaizov
14,228 Points

Love what you did here Marcus and Chris , by letting to figure out answer yourself, very helpful.

I just helped out where I could.