In the Angular documentation for services, I came across this code:
angular.
module('myServiceModule', []).
controller('MyController', ['$scope','notify', function ($scope, notify) {
$scope.callNotify = function(msg) {
notify(msg);
};
}]).
factory('notify', ['$window', function(win) {
var msgs = [];
return function(msg) {
msgs.push(msg);
if (msgs.length == 3) {
win.alert(msgs.join("\n"));
msgs = [];
}
};
}]);
My question is, why not do it much simpler and just define the function notify inside the $scope.callNotify
function?
If services are just functions defined elsewhere, aren't there much simpler ways of accomplishing the same thing?