Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript AngularJS Services and Dependencies Services: Requiring factories

Micah Dunson
Micah Dunson
34,368 Points

Unsure about array notation

I don't understand how to use array notation and how to log it for this exercise

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

angular.module('treehouseCourse')
  // YOUR CODE HERE
.controller('MyCourseCtrl', function(Course) {
  console.log($scope.Course.get());
})
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>

1 Answer

Chris Shaw
Chris Shaw
26,650 Points

Hi ,

The array notation (or dependency injection as it's better known) is quite simple, all you need to do is declare the $scope and Course parameters along with the callback function for the controller within an array, you can read more about this at the below link.

https://docs.angularjs.org/guide/di

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

It might look confusing, but it's very simple, a lot of the time when we're compiling our code for production servers we would obfuscate it which changes the variable names which can break out code, using dependency injection we can define what these variables are and Angular will automatically assign the correct values to the callback function.

Hope that helps.

Micah Dunson
Micah Dunson
34,368 Points

Thank you explaining a little better and the link. Helps a lot.