1

I have a common angular service and multiple controllers. My service is notified of a change which happens in server end. Now i want service to notify the controller of this change so that i can update some controller properties.

I have used $rootscope.$broadcast in service and $scope.$on in controller for achieving this. Is this a good practice to achieve it in this way.

Hershika Sharma
  • 105
  • 1
  • 14
  • there is no problem in using this method. check this you may get a better clarification http://stackoverflow.com/a/28156845/7333443 – Sibiraj May 22 '17 at 07:15
  • Thanks you! Is there any possibility of memory leaks in this method? I am going to use this in multiple controllers in my application. Please suggest. – Hershika Sharma May 22 '17 at 07:34
  • Thanks! One more thing if you could tell me - i sit possible to reload a module's directive without actually reloading the page. Scenario is : i am using 'ui.bootstrap.datepicker' this angular js module for my application. My application is multilingual. If at backend language changes from LTR(english) to RTL (arabic) i need to update my UI without refreshing the page – Hershika Sharma May 22 '17 at 08:42
  • It won't have memory leaks, but make sure that use that only when necessary and avoid unnecessary use of $watch in your system. I am posting an answer regarding do's and don't's – Sibiraj May 22 '17 at 09:35
  • as @Rohit Jindal mentioned. you can use isolated scopes – Sibiraj May 22 '17 at 09:35
  • Check this http://stackoverflow.com/documentation/angularjs/1921/profiling-and-performance#t=201705220936409514068 for performance related stuffs – Sibiraj May 22 '17 at 09:38
  • thanks for the help – Hershika Sharma May 22 '17 at 10:35

1 Answers1

0

First you have to understand where we have to use $rootscope.broadcast.

Instead of using $rootscope.$broadcast you can directly inject the service into the controller and when ever controller is initialized changes into the service will reflect into the controller.

Graham
  • 7,431
  • 18
  • 59
  • 84
Debug Diva
  • 26,058
  • 13
  • 70
  • 123
  • that's correct. But question here is : I want to notify my controller of an event happened in my service. This is after the initial load has happened. – Hershika Sharma May 22 '17 at 07:24
  • If there is any event then you can use `$scope.$watch`. – Debug Diva May 22 '17 at 07:41
  • Thanks! One more thing if you could tell me - i sit possible to reload a module's directive without actually reloading the page. Scenario is : i am using 'ui.bootstrap.datepicker' this angular js module for my application. My application is multilingual. If at backend language changes from LTR(english) to RTL (arabic) i need to update my UI without refreshing the page. – Hershika Sharma May 22 '17 at 08:40
  • Yes you can do by using isolated scope in the directive..you can pass the language as a two way data binding symbol into the scope property of a directive. – Debug Diva May 22 '17 at 09:20
  • thanks for the help – Hershika Sharma May 22 '17 at 10:36
  • You can mark this answer as correct if it was useful for you so that it will help others as well. – Debug Diva May 22 '17 at 10:56