Understanding how controller uses the service


I am confused about how you use a service. I understand how the service is constructed with .success and .error handlers, but I don't get how it's incorporated into the controller.

Here's the service:

app.factory('forecast', ['$http', function($http) {
return $http.get('https://s3.amazonaws.com/codecademy-content/courses/ltp4/forecast-api/forecast.json')
.success(function(data) {
return data;
.error(function(err) {
return err;

Here's what's in the controller:

`app.controller('MainController', ['$scope', 'forecast', function($scope, forecast) {
$scope.fiveDay = data;

}]); `

What is happening in the controller? I have two specific questions about this.
1. Is the forecast.success function the same as defined in the service? If so, it seems like it's calling the success handler directly...how is that permitted? What if there was an error instead?

  1. In the controller's .success function, is it calling success, then passing in the result of that function as the parameter "data?"
    a)This might be a basic JavaScript question, but if the above is true, how does it "know" to pass the result of the success function as the data argument?

Thanks in advance


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.