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 Making Your Own Widget Using factories in a controller

"Using Factories in a Controller" Challenge incorrectly refuses the creation of a controller w/ the array annotation.

For some strange reason, task 2 of the "Using Factories in a Controller" challenge refused to accept/acknowledge the array syntax of creating a controller to account for minification that is also covered in this course by Alex Vanston.

.controller('surveyCtrl', ['$scope', 'User', 'Survey',  function($scope, User, Survey){}])

An obscure inject[0] error pops up I cannot replicate with Plunker, jsBin and so on...

Edit: Minor mistake of retyping controller code + a spelling typo

Edit2: Was lazy with comments with my forum retyping of my answer as pointed out by Ryan Duchene

8 Answers

Link to challenge: http://teamtreehouse.com/library/angularjs-2/making-your-own-widget/using-factories-in-a-controller

Challenge Task 1 of 3

We've defined 'User' and 'Survey' factories on the 'treehouseCourse' module for you. Add a controller called 'surveyCtrl' to the module. The controller should require $scope via dependency injection, as well as the 'User' and 'Survey' factories. You don't need to add any statements in the controller function body yet.

Challenge Task 2 of 3

Within the controller function body, assign the return value from User.get() to $scope.user.

Challenge Task 3 of 3

Within the controller function body, add a statement to assign the return value from Survey.get() to $scope.survey.

After spend house and hours researching through StackOverflow and the AngularJS docs I came up with:

angular.module('treehouseCourse', [])
  // ADD YOUR CONTROLLER TO THE MODULE HERE
.controller('surveyCtrl', function($scope, User, Survey){
$scope.user = User.get();
$scope.survey = Survey.get();
})

Why this work I have no idea, but it passes all three parts of the challenge.. :thumbsup:

This isn't the array notation of defining dependencies at all; this should always work and was covered in the video of this stage actually along with the array notation of defining dependencies.

Was the video addressing this aspect of Angular confusing in a particular way to sepend hours researching it through StackOverflow and the Angular JS docs?

That said, maybe Craig Dennis, Alex Vanston, Dave McFarland, Andrew Chalkley, Aimee Ault, or another amazing person in the Treehouse community or staff that knows JS can help resolve this thread.

This is by far the best answer. The code provided worked perfectly.

.controller('surveyCtrl', function($scope, User, Survey) { })

this work! Thanks Mike

Are you forgetting the commas in-between the array elements? or is that just me?

module.controller('surveyCtrl', [
  '$scope',
  'User',
  'Survey',
  function($scope, User, Survey) {

    // your code here

}]);

Not in my actual challenge answer; as written here in the forums, it would create a parse error.

Mikko Kujapelto
Mikko Kujapelto
4,299 Points

I'm facing the same issue here.

.controller('surveyCtrl', ['$scope', 'User', 'Survey', function($scope, User, Survey) {
}]) 

This raises that inject[0] exception TypeError: 'undefined' is not an object (evaluating 'inject[0]')

jdh
jdh
12,334 Points

Hey Kevin Lozandier,

Did you ever hear back about this? I'm getting the same error message.

Alex Vanston - can you help here please?

Thanks,

Jake

I'm still waiting to hear back about this; Craig Dennis, Dave McFarland, or Andrew Chalkley can probably chime in. Responses to questions have been slower than usual as people finish up their respective holiday breaks.

Don't mistake the waiting time for this question being answered being the norm at all. :)

Jason Early
Jason Early
52,944 Points

I'm getting the parse error as well. What am I missing?

.controller('surveyCtrl',[$scope, User, Survey, function($scope, User, Survey) {

}]);

jdh
jdh
12,334 Points

Any update on this?

I can't remember if this was covered earlier in this Treehouse course or not, but I believe including the quoted dependences in an array before the callback function is done so that the Angular doesn't break during minification.

Like James noticed, it works if you DON'T include the array before the callback function, and just go straight to function:

.controller('surveyCtrl', function($scope, User, Survey) {
  })

While this breaks:

.controller('surveyCtrl', ['$scope', 'User', 'Survey', function($scope, User, Survey) {
  }])

I'm not sure if this is a little bug or not.