StackOverflow_24595460: angularjs-how-to-update-rootscope-after-binding-rendering-has-occurred

Illustration of answer to http://stackoverflow.com/questions/24595460/angularjs-how-to-update-rootscope-after-binding-rendering-has-occurred.

by ExpertSystem

HTML

<script src="http://code.angularjs.org/1.2.19/angular.min.js"></script>
<div>
    <p>{{someText}}</p>
    <button onclick="badPractice()">This button is just BAD practice</button>
</div>

JavaScript

var app = angular.module('myApp', []);
app.run(function ($rootScope) {
    $rootScope.someText = 'Hello, world !';
});

function badPractice() {
    var $body = angular.element(document.body);
    var $rootScope = $body.scope().$root;
    $rootScope.$apply(function () {
        $rootScope.someText = 'This is BAD practice, dude ! :(';
    });
}