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 Basics (1.x) Services in Angular Using Services To Get Data

Jason S
Jason S
16,247 Points

$scope.todos is undefined since the data is received after it's been created ?

hi he says this in the video but I do not quite understand what that means. Can someone explain in more detail?

4 Answers

Erik Nemesis
Erik Nemesis
13,356 Points

I didn't see the video but it probably refers to asynchronous data.

Because this is an asynchonous operation, that means that the contents will be available at a later time (it could be in a second or in a few minutes depending on external factors such as connection bandwidth and such), therefore when $scope.todos is created, it is empty at first.

Jason S
Jason S
16,247 Points

okay. so in reference to the video, I am thinking that the $http.get is taking a long time, therefore the response.data is also taking a long time, and dataService.getTodos is undefined because there is no data.

Charles Cloud
Charles Cloud
6,134 Points

My understanding is that the variable is created before the response has been returned, therefore it is set to undefined.

I can understand that the variable is undefined at first. But what happens after function returns something. Should not it be defined?