Eldar Amantai2014-08-26 13:55:16
Eldar Amantai, 2014-08-26 13:55:16

AngularJS - how to properly organize requests in different controllers and directives?

Question about the correct organization of ajax requests. Situation:
There is MainCtrl , the main controller at the html tag level - existing on every page of the site.
There is a CatalogCtrl , a controller in the product catalog in the store.
For MainCtrl , in ng-init, a request is made to get information about the user (name, items in the cart, guest / authorized)
For CatalogCtrl , in ng-init, a request is made to get catalog filters and other additional information, then based on filters there is a request to receive goods
Question - is it right?) Requests are sent not through services, but to $scope.loadItems = function(){ $http() /*... */ }and things like that. How to properly organize requests for information in different controllers?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
kompi, 2014-08-26

Move MainCtrl to the service, since in fact the information inside is common and should be available for different controllers.
Some extra. data can be injected via a constant/value connection for individual pages, or directly injected into a service via an injector.
It is desirable to transfer all requests to services.

Sergey, 2014-08-26
Protko @Fesor

no. it is not right. at least injecting the $http service into controllers is already a dubious decision, but what you are doing is really bad. You have a module's run method, you have controllers... why put it in templates? This is not their area of ​​responsibility.
ng-init was meant to be a helper for ngRepeat, it's not recommended to use it for anything else.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question