javascript - Separation of concerns in angular, when to use a service and/or factory? -
I have recently got to catch with kangaroo and trying to bring my head around the concepts It has been made who has made it. So far everything is secret, but I am going there!
Nevertheless, I have created a small part of my web site using the Compagieer (it looked like fit in use because I needed to manipulate Javascript object on the browser).
As part of this process, I needed to add an object to my model (on the click of an element)
Now I ended up with the code given below: < / P>
var stpApp = angular.module ('stpApp', []); StpApp.controller ('multiStopController', function ($ scope, $ compilation, $ http) {$ scope.inboundJourney = [['Depart departure': '', 'Destination realport': '', 'Sign out': ' ',' Departure '': 9, 'orbit': 'class'}] $ $ scope.addInboundJourney = function ($$ scope.inboundJourney.push ({'DepartureAirport': '', 'DestinationAirport': '', 'DepartureDate': '', 'Departure time': 9, 'Class': ''});}}); and some markup:
& lt; li ng- repeat = "travel in inbound Journey"> & lt; / li & gt; & lt; p class =" addMultiStop "& gt; & lt; span title =" Add a tour "ng-click =" Addinbound Journey () "& gt; By reading a little bit more I have used the services and factories . So should I have the addInboundJourney function in a service or factory, or is it okay in the controller, as it is now? If it is okay in the controller, then when is it advised to use a service and / or factory?
This is a very subjective question if you want to handle manipulation in more than one controller, then inbound mail Take the manipulation into a service and put it in the controllers.
If you are ever going to manipulate the Inside Journey in the MultistopController, then I can say that the better you were, the better.
If you think there is a lot of code for inbound Journey manipulation (you also want to do some other things in the multistopunk controller, by making your controller fat), then I will take it into a service - its Regardless of how many controllers I want to use the functionality.
Comments
Post a Comment