Many angular examples use simple global functions. Angular-seed and yo angular use a more complex coding style. Here is an example of a simple styled example from stackoverflow/jsbin: http://jsbin.com/lucit/8/edit
var app = angular.module( 'app', [] );
var MyFunc = function() {
this.name = "default name";
this.$get = function() {
this.name = "new name"
return "Hello from MyFunc.$get(). this.name = " + this.name;
};
return "Hello from MyFunc(). this.name = " + this.name;
};
// returns the actual function
app.service( 'myService', MyFunc );
// returns the function's return value
app.factory( 'myFactory', MyFunc );
// returns the output of the function's $get function
app.provider( 'myProv', MyFunc );
function MainCtrl( $scope, myService, myFactory, myProv ) {
$scope.serviceOutput = "service = "+ JSON.stringify(myService);
$scope.factoryOutput = "factory = " + myFactory;
$scope.providerOutput = "provider = " + myProv;
}
What follows is an attempt to rewrite a simple example in the angular-seed module style: http://jsbin.com/hitun/4/edit
var MyFunc = function() {
this.name = "default name";
this.$get = function() {
this.name = "new name"
return "Hello from MyFunc.$get(). this.name = " + this.name;
};
return "Hello from MyFunc(). this.name = " + this.name;
};
angular.module('app', [
'app.controllers',
'app.services'
]);
angular.module('app.services',[])
.factory('MainData', MyFunc)
.service('MainData', MyFunc)
.provider('MainData', MyFunc));
angular.module('app.controllers',[])
.controller('MainCtrl', [function($scope, myService, myFactory, myProv){
$scope.serviceOutput = "service = "+ JSON.stringify(myService);
$scope.factoryOutput = "factory = " + myFactory;
$scope.providerOutput = "provider = " + myProv;
}]);
No matter how I try to tweak the syntax, I get errors.