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: 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,676 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.