简体   繁体   中英

Best practice to structure service/factory in angularjs

I was studying angularJs last week and there's something I'm not sure about.

For example, in a project that has some CRUDs of Student, Teacher and Supplier. Is it a good practice split the services/factory for each models (student, teacher and supplier)? or Is it better use one generic service/factory for the same models, like "write once, and run in everywhere"?. I think the second option maybe works for big projects, because you can write less code, but I have no idea about the maintenance.

Obs: The service/factory reffered above has functions with $http to list, add, edit and delete a registry from database.

And Is there any detailed style guide with best pratices for AngularJs?

Thanks in advance!

If you can manage the API endpoints at the back-end side you can create your services based on $resource . All you need (at the front-end side) is to set the endpoint URL. By call of $resource predefined methods like get , save , delete and etc. (you can add your custom methods as well) the $resource will submit HTTP requests with particular HTTP methods (GET, PUT, POST, DELETE) to the defined API endpoint.

If you want to stick to angularJS I would advise you to check this angularjs style guide from Todd Motto, it's most likely best one online: https://github.com/toddmotto/angularjs-styleguide

Really has best practices using component based architecture. You will improve your code drastically and even merging to Angular would be easier.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM