2 alternatives to completely avoid the problem:
<span ng-bind="expression_without_braces"></span>
It is preferable to use ngBind
instead of {{ expression }}
if a template is momentarily displayed by the browser in its raw state before AngularJS compiles it. Since ngBind
is an element attribute, it makes the bindings invisible to the user while the page is loading.
- use
ui-router
to keep HTML in separate files, which will be loaded/parsed/injected only when needed and only when the controller is initialized.
Here's the hello world tutorial modified to use a separate HTML file:
// helloworld.js
var myApp = angular.module('helloworld', ['ui.router']);
myApp.config(function($stateProvider) {
var helloState = {
name: 'hello',
url: '/hello',
templateUrl: 'helloworld.html'
}
$stateProvider.state(helloState);
});
<!-- helloworld.html -->
<h3>hello world!</h3>
<!-- index.html -->
<html>
<head>
<script src="lib/angular.js"></script>
<script src="lib/angular-ui-router.js"></script>
<script src="helloworld.js"></script>
</head>
<body ng-app="helloworld">
<a ui-sref="hello" ui-sref-active="active">Hello</a>
<ui-view></ui-view>
</body>
</html>