Search code examples
angularjsangularjs-serviceangularjs-controller

Communicate to another, specific controller


I'm a bit lost... I'm quite new to Angular and writing a small app for practicing.

I've two views with a table and each view has it's own controller. The tables use some equal functions, like edit, save... To avoid duplicate code I've put these functions in a seperate controller. Now if in one view e.g. the data in a table-row is edited and saved I want to fire an event to inform the specific controller for that view and send the new object with that event. I already got that running but not sure if it is the right way to do it...

So right now my TableEditController is parent of each table-view-specific Controller and I can send the event with $emit. By doing some research I've read, that you should use a service for the communication, but in the examples $rootScope is used and I think that's not the right way to do it, as I only want to inform the specific controller about the change so that it can do further handling (like saving to database or whatever).

Any thoughts or ideas?


Solution

  • Put your business logic in a service and share the service with you controllers to avoid code duplication.

    If you have the same controller methods as well (and a lot of them) , then create a parent controller (or inherit via prototype) with the same logic.