Using Ionic v1, I am trying to call a function in my controller when an event listener in my app.run fires. The event is created by a Cordova (Phonegap) plugin that detects changes in the headphone jack.
I must be doing something wrong because I can't find a solution that works. Here is my example:
(function() {
var app = angular.module('mymodule', ['ionic']);
app.run(function($ionicPlatform) {
$ionicPlatform.ready(function() {
if(window.HeadsetDetection){
/*Monitor for changes in the headphone jack*/
document.addEventListener('deviceready', function() {
window.HeadsetDetection.registerRemoteEvents(function(status){
// *HERE* is where I want to call or somehow trigger
an action in my controller
});
}, false);
}
});
});
When the headphone jack is sensed I am trying to close an ionic popup created in my controller.
You are defining your events from .run
part of your angular application.
Therefore; to communicate with a controller; you need to use : - a global variable ($rootScope) on your app that is watched in your controller : this is an ugly solution; that I do not recommand - Broadcast / on an event in your app: this is the clean solution
You need ; when you detect the event to broadcast to your app the news :
$rootScope.$broadcast('headphones-Updated');
Within your controller; you have juste now to catch the event :
$scope.$on('headphones-Updated', function(event, args) {
});
Another solution would have to set your event listener right in the controller