Good practices with Angularjs controllers

Asked

Viewed 1,511 times

0

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

In the back-end I would search the data of PessoasController, But as far as I can tell, it’s not possible to access more than one controller methods in each section of the view. Besides, I don’t think a registration form for usuarios using something like ng-controller=PessoasController.

Forgive me for being inexperienced, but as you would do?

1 answer

1

As far as I’m concerned, you have a baggage loaded with traditional backend MVC.

If I understand correctly, you’re talking about a relational entity, a database, in which User has a 1-1 relationship with Person, I’m sure?

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 two different entities.

But, to solve your question, it is possible to access data from another controller in Angularjs since you access through a child controller the scope of parent controller.

I 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.

https://jsfiddle.net/luishmcmoreno/e6gj4bcs/

To facilitate access, the syntax was used controller the to better reference each scope. You can access it via context this controller. If you don’t know how controller the, search on the internet that has good references.

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

https://github.com/angular/angular.js/wiki/Understanding-Scopes

The other way you share information across scopes is by using Factory or service. Angular is considered a framework MVW or MV* by many users. Precisely because it has more layers like services, Factories, Filters, among others.

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

I created another jsfiddle to demonstrate the sharing of information between controllers. Below.

https://jsfiddle.net/luishmcmoreno/hs8pdm47/

Just to 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 the parent and child scope works and how information sharing works using other layers of the application, such as service.

I suggest you practice hard. To follow 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 the layers of an angular application work: module, config, Resource, run, controller, service, Factory, filter.

https://github.com/johnpapa/angular-styleguide/blob/master/a1/i18n/pt-BR.md

  • Perfect, Luis. I really need to study these layers. The back-end approach to MVC is due to a framework I’m currently studying. But how would you handle this case? Would you have both controllers and use them as in your same example? Thank you.

  • I didn’t quite understand what the real need was. In this case I would abstract the server business rules from the application and, in Angularjs would have a json already embedded all user and person information, without having to separate them into two controllers. But actually, I didn’t understand the need because I don’t know what the business rule is. Sometimes you need to separate into two controllers but I believe that.

  • This is a user registration form. In this form there is a select field with the list of people registered in the system. I want to get the email from the selected person and display it in the email field in the user’s registration. The question regarding the controller arose from the fact that I deal with two entities in the same form.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.