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?