0

In the following two examples I am using 2 different structures for service return. While first I am returning the function in braces, in second I am returning function by function name. According to me both should work fine.

While Case 1 works, Case 2 fails. Can someone explain why?

Case 1

// module declaration
var app = angular.module('myApp', []);

// controllers declaration
app.controller('myCtrl', function($scope, baseService ){
$scope.name = "Peter";
var a = baseService.helloFunction()
	console.log(a);
});

// services declaration
app.service('baseService', function($http){
return{
	// For Rating Chart 
	helloFunction: function() {
		return "Hello";
	}

}

});
<body ng-app="myApp" ng-controller="myCtrl"> 
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.5/angular.min.js"></script>
</body> 

Case 2:

//module declaration
var app = angular.module('myApp', []);

//controllers declaration
app.controller('myCtrl', function($scope, baseService ){
$scope.name = "Peter";
var a = baseService.helloFunction()
	console.log(a);
});

// services declaration
app.service('baseService', function($http){

// For Rating Chart 
helloFunction: function() {
	return "Hello";
}

return helloFunction();

});
<body ng-app="myApp" ng-controller="myCtrl"> 
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.5/angular.min.js"></script>
</body> 

In this, while Case 1 works fine, Case 2 fails! While in both cases in service structure I am returnign inner function in outer function. Why doesn't case 2 works?

6
  • first one is service second one is factory Commented Jan 4, 2017 at 6:29
  • posibble duplicate of this stackoverflow.com/questions/41425879/… Commented Jan 4, 2017 at 6:30
  • Both are factories only. Commented Jan 4, 2017 at 6:30
  • No, completely different question that was. Commented Jan 4, 2017 at 6:31
  • but the solution you are looking for is same Commented Jan 4, 2017 at 6:31

2 Answers 2

2

You case 2 has syntactical errors and You are returning the output of the function helloFunction. As you are invoking it.

Since string does't have helloFunction() function it throws the error.

Here the snippet.

//module declaration
var app = angular.module('myApp', []);

//controllers declaration
app.controller('myCtrl', function($scope, baseService) {
  $scope.name = "Peter";
  var a = baseService.helloFunction()
  console.log(a);
});

// services declaration
app.service('baseService', function($http) {
  // For Rating Chart 
  this.helloFunction = function() {
    return "Hello";
  }
});

//Or, Create factory
app.fatory('baseService2', function($http) {
  // For Rating Chart 
  var helloFunction = function() {
    return "Hello";
  }
  return {
    helloFunction: helloFunction
  };
});
<body ng-app="myApp" ng-controller="myCtrl">
  <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.5/angular.min.js"></script>
</body>

Sign up to request clarification or add additional context in comments.

1 Comment

your code snippet in not correct. service does not suppose to return any explicit object in angular
2

You service code should look like this

//module declaration
var app = angular.module('myApp', []);

//controllers declaration
app.controller('myCtrl', function($scope, baseService ){
$scope.name = "Peter";
var a = baseService.helloFunction()
	console.log(a);
});

// services declaration
app.service('baseService', function($http){

// For Rating Chart 
this.helloFunction: function() {
	return "Hello";
}
  
});

Or the way you are using is suppose to be factory

//module declaration
var app = angular.module('myApp', []);

//controllers declaration
app.controller('myCtrl', function($scope, baseService ){
$scope.name = "Peter";
var a = baseService.helloFunction()
	console.log(a);
});

// services declaration
app.factory('baseService', function($http){

return { 
helloFunction: function() {
	return "Hello";
}

  }


});

Services are like prototype function no need to return explicitly and in case of factory you return the object.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.