Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where should I put AngularJS Factories & Services?

I'm working to cleanly structure my AngularJS app according to best practices, which includes separating the controllers and app into different script files.

Quick question: where should I put my factories and services? I am asking in the context of having factories & services that will be accessed outside of the scope of a single controller as well as having some that are within the scope of a single controller.

like image 355
Jordan Thornquest Avatar asked Apr 05 '13 16:04

Jordan Thornquest


People also ask

What are factories in AngularJS?

A factory is a simple function which allows us to add some logic to a created object and return the created object. The factory is also used to create/return a function in the form of reusable code which can be used anywhere within the application.

Where do I run AngularJS code?

Running the Development Web Server While AngularJS applications are purely client-side code, and it is possible to open them in a web browser directly from the file system, it is better to serve them from an HTTP web server.

In which directory is AngularJS most important files stored?

src folder: This is the folder which contains the main code files related to your angular application.

Is Google still supporting AngularJS?

And the most crucial aspect is that AngularJS support has officially ended as of January 2022.


1 Answers

Update: the immediate answer below is no longer correct. Please see the latest addendum (written March 1, 2015) to this answer.

Got it! According to Brian Ford's article on Building Huuuuuuuge Angular Apps, the best practice appears to be to connect services and factories to the app in a separate file, like so:

root-app-folder
├── index.html
├── scripts
│   ├── controllers
│   │   └── main.js
│   │   └── ...
│   ├── directives
│   │   └── myDirective.js
│   │   └── ...
│   ├── filters
│   │   └── myFilter.js
│   │   └── ...
│   ├── services
│   │   └── myService.js
│   │   └── ...
│   ├── vendor
│   │   ├── angular.js
│   │   ├── angular.min.js
│   │   ├── es5-shim.min.js
│   │   └── json3.min.js
│   └── app.js
├── styles
│   └── ...
└── views
    ├── main.html
    └── ...

(PSST! In case you're wondering, Brian Ford is part of the AngularJS team, so his answer seems pretty legit.)

Addition (April 24, 2013)

This just in: Yeoman is a fantastic tool for generating apps with the proper directory structure for big, functional Angular apps. It even has Grunt & Bower packed in!

Addendum (March 1, 2015)

According to a comment via PaoloCargnin, Google actually recommends a different structure, as detailed by this document. The structure should look like this:

sampleapp/
    app.css
    app.js //top-level configuration, route def’ns for the app
    app-controller.js
    app-controller_test.js
    components/
        adminlogin/                                
            adminlogin.css //styles only used by this component
            adminlogin.js //optional file for module definition
            adminlogin-directive.js                         
            adminlogin-directive_test.js        
        private-export-filter/
            private-export-filter.js
            private-export-filter_test.js
        userlogin/
            somefilter.js
            somefilter_test.js
            userlogin.js
            userlogin.css                
            userlogin.html                
            userlogin-directive.js
            userlogin-directive_test.js
            userlogin-service.js
            userlogin-service_test.js                
    index.html
    subsection1/
        subsection1.js
        subsection1-controller.js
        subsection1-controller_test.js
        subsection1_test.js                         
        subsection1-1/                        
            subsection1-1.css
            subsection1-1.html
            subsection1-1.js
            subsection1-1-controller.js
            subsection1-1-controller_test.js
            subsection1-2/                        
    subsection2/
        subsection2.css
        subsection2.html
        subsection2.js
        subsection2-controller.js
        subsection2-controller_test.js
    subsection3/
        subsection3-1/
etc...
like image 111
Jordan Thornquest Avatar answered Oct 21 '22 09:10

Jordan Thornquest