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 Writing Your Own Service/Factory

carine todmia
carine todmia
6,071 Points

People.get()

in the example why was the controller written with People.get() with the parentheses but People.add and People.remove etc were written without it?

3 Answers

Ben Griffith
Ben Griffith
5,808 Points

When you use parenthesis - the function (in this case People.get()) will get executed straight away and the return value assigned to $scope.people

Since we want to get the list of people straight away, this makes sense right?

$scope.people = `People.get()

The others don't have parenthesis because we don't want to execute the function right there and then. All we want to do is assign the actual function to $scope so that we can call it at a later date.. for example once we've clicked on the remove button.

Hope that clears it up.

Daniel Niclas
Daniel Niclas
22,300 Points

I agree with Ben.

$scope.people = PeopleFactory.get();
// this METHOD needs to be invoked by the controller to get the collection from the factory as part of initialization. (Even though the ARRAY is empty).

$scope.people = PeopleFactory.add; // NO parenthesis

$scope.people = PeopleFactory.remove; // NO parenthesis

// These METHODS are only invoked when ng-click="add(person)" or ng-click="remove(person)" events are triggered by clicking on the respective button element

I believe each need to have the parens, I think this was just an oversight in the video.