Monday 15 August 2011

angularjs - In Angular, what's the point of a service? -



angularjs - In Angular, what's the point of a service? -

in angular documentation services, came across code:

angular. module('myservicemodule', []). controller('mycontroller', ['$scope','notify', function ($scope, notify) { $scope.callnotify = function(msg) { notify(msg); }; }]). factory('notify', ['$window', function(win) { var msgs = []; homecoming function(msg) { msgs.push(msg); if (msgs.length == 3) { win.alert(msgs.join("\n")); msgs = []; } }; }]);

my question is, why not much simpler , define function notify within $scope.callnotify function?

if services functions defined elsewhere, aren't there much simpler ways of accomplishing same thing?

just think resuing code in controller; not able it.

but if place in service, can injected , reused everywhere.

angularjs service

No comments:

Post a Comment