SO - 15424910
HTML
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<div ng-app>
<div id="outer" ng-controller="MsgCtrl">
You are {{msg}}
</div>
<div onclick="change()">click me</div>
</div>
JavaScript
function MsgCtrl($scope, $rootScope)
{
$scope.msg = "great";
$rootScope.safeApply = function( fn ) {
var phase = this.$root.$$phase;
if(phase == '$apply' || phase == '$digest') {
if(fn) {
fn();
}
} else {
this.$apply(fn);
}
};
}
function change() {
var scope = angular.element($("#outer")).scope();
scope.safeApply(function(){
scope.msg = 'Superhero';
})
}