エラーをスローすることなく、以下のスニペットにスコープを適用する方法はありますか? (そして、ハックや回避策try/catch
、$timeout
またはBONJOURのハードコーディングなし)
がないSCOPE.$apply()
場合、アラートは{{HELLO}}
の代わりに表示されBONJOUR
ます。
var app = angular.module('APP', [])
.controller('CTRL', function($scope, $compile) {
$scope.showBonjour = function() {
var SCOPE, CONTENT;
SCOPE = $scope.$root.$new();
SCOPE.HELLO = 'BONJOUR';
CONTENT = $compile('<div>{{HELLO}}</div>')(SCOPE);
SCOPE.$apply(); // This generates the $rootScope:inprog error, but I cannot omit it…
window.alert(CONTENT.html());
}
});
<script src="https://code.jquery.com/jquery-2.1.3.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.13/angular.min.js"></script>
<html ng-controller="CTRL" ng-app="APP">
<button ng-click="showBonjour()">Show BONJOUR</button>
</html>