Good practices with AngularJS controllers

0

In a view responsible for registering members of the usuarios entity, if it is necessary to access data of a pessoas entity, for example, the ideal method is that the method that will search the data of this pessoa belongs to something like UsuariosController or PessoasController ?

In the back end I would search for PessoasController data, but as far as I can see, it is not possible to access methods from more than one controller in each section of the view. Also, I do not seem to make sense of a usuarios registration form using something like ng-controller=PessoasController .

I'm sorry for the inexperience, but how would you do it?

    
asked by anonymous 02.04.2016 / 05:42

1 answer

1

As I see it, you have a traditional MVC loaded luggage from the backend.

If I understand you, you are talking about a relational database entity in which User has a 1-1 relationship to Person , am I right? p>

If yes, the idea in this case is that you abstract this on the server and the application does not need server implementation details, receiving a single object without differentiating between two different entities.

But to answer your question, you can access data from another controller in AngularJS as long as you access the parent controller scope through a child controller .

I have created a very simple example in JS Fiddle, using its Person and User logic, in which the user scope is within Person, in the link below.

link

For ease of access, the controller as syntax was used to better reference each scope. You can access it via the this context of the controller. If you do not know how controller as works, search the internet for good references.

For a more in-depth study I strongly suggest reading the link below, which explains in detail how $ scope works in the angular.

link

The other way for you to share information between scopes is by using factory or service . The angular is considered an MVW or MV * framework by many users. Precisely because it has more layers like services, factories, filters , among others.

In this case, it is not necessary for scopes to be parent and child . This is the most recommended way to share information between controllers, because it avoids code reuse.

I've created another jsfiddle to demonstrate the sharing of information between controllers. See below.

link

Just point out that these two examples were created for didactic purposes and do not follow the best angular practices. These examples are designed to help you better understand how parent and child scope works and how information sharing works by using other layers of the application, such as service.

I suggest you practice hard. To follow the best development practices with Angular, read John Papa's excellent style guide, translated into Portuguese. However, I advise you to read after you have understood, at least superficially, how all layers of an angular application work: module, config, resource, run, controller, service, factory, filter.

link

    
02.04.2016 / 08:41