How do I organize my Angular code?

How do I organize my Angular code?

Plugin to indent in visual studio code

A module is one of the main elements with which we can organize the code of applications in Angular. They should not be unknown until this point of the Angular Manual, since our basic application already had one.

However, instead of placing the code of all components, directives or pipes in the same main module, it is appropriate to develop different modules and group different elements in one or the other. The order will be done in a logical way, according to our own preferences, the business model or the preferences of the development team.

To facilitate the tasks of module creation we will rely on the Angular CLI. The command to generate this new module is “generate” and then we have to indicate what we want to generate, in this case “module”, ending with the name of the module to create.

Once this command is launched in our project, inside the “src/app” folder a subdirectory with the same name of the generated module is created. Inside we will also find the file with the module code.

Indexing visual studio code

Thinking about how to start an open source project? congratulations! The world appreciates your contribution. Let’s talk about what an open source project is and why people are doing it.

When a project is open source, it means that anyone can view, modify, use or distribute your project for any purpose. These permissions are enforced through an open source license.

See also  Where are Lalafell from?

The result is that most projects of this type are free, but “free” is not part of the definition of “Open Source”. There are ways to charge for these projects indirectly through dual licensing or limited functionality, while still meeting the official definition of “Open Source”.

The goals can help you detect where to continue working, what to say no to, and where to turn for help. Start by asking yourself, why am I “open-sourcing” my project?

As your project grows, your community may come to need more than just the code. That is, it will need you to respond to issues, review the code, among other important tasks in such a project.

Angular project structure

Here is a community-driven angularJS best practices page. Their file structure suggestion is a combination of both ideas in two examples. I like the second example better, as it is more behind the idea of sorting by function.

More importantly, by keeping that structure, it helps you separate your client-side code and make it independent of server-side technology (all sorts of different folder structures and different server-side template engines).

+1 Plus, you can customize where yeoman puts your stuff. In my humble opinion, sorting by function/module in large applications is better as you can more easily reuse a function in other applications.

There is also the approach of organizing folders not by framework structure, but by application function structure. There is a github startup Angular / Express application that illustrates this called angular application .

You will have trouble maintaining and introducing new features later on. It is always a good practice to have a folder structure with features and components related to that feature under your folder.

See also  Should file names be capitalized?

Visual studio code ordering code

An AngularJS application is made up of controllers, and each controller encompasses a part of the application. Angular allows you to manage what is known as SPA (Single Page Applications), among its main modules is the routing module, which allows you to define if it is a valid route, the view and the controller to use for that route.

The connection between the controller and the view is the $scope, which are like objects that are shared between the view (html templates) and the models, something like what they call ViewModel in the MVVM pattern, every time you update a model in a controller this value is updated in the view as well.

AngularJS offers modules to organize your code, it is an excellent tool, in fact you can create modules and include them inside others, this often helps to better divide the project files.

Note: When you create a module is done with the angular.module function, it receives the name of the module and its dependencies, in case there are no dependencies you must pass an empty array, it is very important this parameter or your module does not work.

See also  What is the structure of horror?