Imagine I have a controller which handles, for example, view changes:
function Controller($scope){
var viewModel = this;
viewModel.goBack= function(){
viewModel.visible = visibleLinks.pop(); //get last visible link
viewModel.swipeDirection = 'left';// for view change animation
}
}
But I want to handle it not only for example with HTML buttons inside <body>
, but also with Back button on device. So I have to add Event Listener for deviceready
event, and also explicit call $scope.$apply()
in order to fact, that it is called outside of AngularJS context, like this:
document.addEventListener("deviceready", function(){
document.addEventListener("backbutton", function(){
viewModel.goBack();
$scope.$apply();
}, false);
}, false);
}
But I also want to follow (relatively :) ) new controllerAs
syntax, cause this is recommended now e.g. by Todd Motto: Opinionated AngularJS styleguide for teams and it allows to remove $scope
from controllers when things like $emit
or $on
are not used. But I can't do it, case I have to call $apply()
cause my context is not Angular context when user clicks on device back button. I thought about creating a Service
which can be wrapper facade for cordova and inject $scope
to this service but as I read here: Injecting $scope into an angular service function() it is not possible. I saw this: Angular JS & Phonegap back button event and accepted solution also contains $apply()
which makes $scope
unremovable. Anybody knows a solution to remove Cordova specific events outside Angular controller, in order to remove $scope
from controllers when not explicity needed? Thank you in advance.