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 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.