We want to embed HTML with ng-bind-html-unsafe, therefore it ng-clickdoes not work. For it to work, we need to compile this source using the service $compile.
So, let's create a “compiler”:
.directive( 'compileData', function ( $compile ) {
return {
scope: true,
link: function ( scope, element, attrs ) {
var elmnt;
attrs.$observe( 'template', function ( myTemplate ) {
if ( angular.isDefined( myTemplate ) ) {
elmnt = $compile( myTemplate )( scope );
element.html("");
element.append( elmnt );
}
});
}
};
});
after, create a dummy factorythat mimics a response from the server:
.factory( 'tempService', function () {
return function () {
return '<form class= "form-signin" >' +
'<h2 class="form-signin-heading">Please sign in</h2>' +
'<input type = "text" class= "input-block-level" placeholder = "User name" ng-model = "username" >' +
'<input type="password" class="input-block-level" placeholder="Password" ng-model="password">' +
'<label class="checkbox">' +
'<input type="checkbox" value="remember-me" ng-model="remember"> Remember me' +
'</label>' +
'<input type="button" class="btn btn-large btn-primary" ng-click="login()" value="Sign in">' +
'</form>';
};
});
And finally, add our directive in HTML:
<div compile-data template="{{mainPage}}"></div>
, factory directive:
$scope.mainPage= tempService();
: FIDDLE