Angular, use same Cope between controls

Asked

Viewed 303 times

3

A question for an expert in angular, for me to use a Scope in several controllers of a module, I can only with service(factory) and or rootScope? I’m in a bit of a bizarre situation. We have a project that the customer has a problem in the database, it takes on average 40 seconds for the bank to present a new data inputado in the call get. They want me to put the dry dice in the Cope of the other controllers, imagine, it’s going to be a lot of problems. In addition to Resources and the entity that makes the get/post, I will have to keep adding in the 'dry' the Scopes between the controllers, cruds, modulos. It’s going to be a zone, full of Ctrl c and Ctrl v, someone has some hint for me to get out of this mess, it’s going to be awful :(.

  • If what you need is to share data across scopes, the ideal is to centralize that data into a service and the scopes only reflect that data. Sharing scopes is an anti-pattern.

1 answer

5


Use a service to contain the values to be shared.

When it is necessary that one of the controllers change a value, use a service store the value locally and use $scope.$broadcast to issue a warning to all controllers the value has been changed.

Within each controller, utilize $rootScope.$on to monitor changes and update the $scope local.

Browser other questions tagged

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