What is the proper way to use factories/services in AngularJS? -
i have basic factory looks this:
mmodule.factory('ajax_post', ['$http', function(_http) { return{ init: function(jsondata){ var _promise= _http.post('src/php/data.ajax.php', jsondata ,{ headers: { 'soapactions': 'http://schemas.microsoft.com/sharepoint/soap/updatelistitems' } } ); return _promise; } }]); and here call in controller:
ajax_post.init($scope.jsondata) .then(function (result) { if(result.status == 200){ $scope.isdone = true; .... } }, function (error) { alert(error.message); }); i want use $scope other purposes here, seems can use parent (aka $rootscope). have 1 controller.
so here questions:
- is practice use
$scopein factory/service, or should controller must use (because according mvc, know$scoperepresentsviewcurrentmodel). - can ignore
returnin factory/services, or should must return something? - can implement
promise.then(..)factory/service (meaning: can put above call controller factory/service)? or right way create promises , call them controllers? - why should not implement aforementioned
servicelogic incontrollerbody? - can write several methods call each in 1 service?
is practice use $scope in factory/service, or should controller must use (because according mvc, know $scope represents view current model).
avoiding modifying $scope , doing 1 thing imho better reusability.
can ignore return in factory/services, or should must return something?
you must return object you're gonna use, here.
can implement promise.then(..) factory/service (meaning: can put above call controller factory/service)? or right way create promises , call them controllers?
you can pass scope in .init code, you're coupling directive controller
why should not implement aforementioned service logic in controller body?
the controller should not know how query microsoft db. controller's role "prepare data", not obtain them.
can write several methods call each in 1 service?
which do?
Comments
Post a Comment