Synchronise controller and service

24 views
Skip to first unread message

Yehonathan Sharvit

unread,
Apr 6, 2014, 3:34:49 AM4/6/14
to ang...@googlegroups.com
I have a service that is connected to the microphone (navigator.webkitGetUserMedia) and calculates the microphone volume ~ every 20 msec.
I would like to access the calculated volume from two controllers.

The only solutions I found are:
1. to use $interval from inside the controller. But it takes a lot of CPU resources.
2. to fire an event from the service and listen to the event from the two controllers.


Any other suggestions?

Tony pee

unread,
Apr 6, 2014, 4:16:37 AM4/6/14
to ang...@googlegroups.com
as long as the interval which accesses webkitGetUserMedia calls $rootScope.$apply() angular refresh watches when the service calculates the volume. You can then just reference the calculated value directly from the controllers, and when it changes, they will be updated, due to angulars databinding. 

no?


--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.



--
Tony Polinelli

Tony pee

unread,
Apr 6, 2014, 4:18:21 AM4/6/14
to ang...@googlegroups.com
oh, but if you want to call code in the controller, not just reference the value in the template (partial). then you will want to use a $watch in the controller code
--
Tony Polinelli

Yehonathan Sharvit

unread,
Apr 6, 2014, 9:44:02 AM4/6/14
to ang...@googlegroups.com
Thanks Tony.
I have implemented the $rootScope.$apply inside the service.
Then, I have figured out that my CPU consumption was due to the fact that $interval calls $root.$digest. So I have modified my code so that it calls $scope.$digest which is much less CPU expensive.


You received this message because you are subscribed to a topic in the Google Groups "AngularJS" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/angular/2YTE29npJHI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to angular+u...@googlegroups.com.

To post to this group, send email to ang...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.



--
"Are we what we become or do we become what we are?" - Prof. Beno Gross
Reply all
Reply to author
Forward
0 new messages