3

I have html template like this:

$scope.template = '<span class="pointer"><i class="icon-refresh pointer" ng-click="refresh()"></i></span>'; 

I want to bind this template using ng-bind-html, I tried to use it and also I used ng-bind-html-unsafe, but unfortunately It bind the html string as it is with no click action.

<span ng-bind-html="template"></span> <span ng-bind-html-unsafe="template"></span> 

I read about similar problem and it said that ng-click is loaded after ng-bind, so can anybody introduce to me how to solve this problem?

4
  • 1
    The fact Angular makes it so difficult to do this highlights exactly why you shouldn't do it. HTML does not belong in the Controller. Pull it in via the View Commented May 27, 2014 at 12:27
  • Why do you need to have a template in a JS variable ? Commented May 27, 2014 at 12:53
  • In the project I'm working on I have to bind templates to sub-views in the main view, so I need to bin the template in the required panel (sub-view) with its functions Commented May 27, 2014 at 13:03
  • What version of Angular are you using ? Commented May 28, 2014 at 6:22

2 Answers 2

2

Perhaps you need to compile the template inside the controller?

angular.controller('ABCDCtrl', function($scope, $compile){ var templateHTML = '<span class="pointer"><i class="icon-refresh pointer" ng-click="refresh()"></i></span>'; $scope.template = $compile(templateHTML)($scope); }); 
Sign up to request clarification or add additional context in comments.

Comments

1

You could try ng-include and put your template in to a static file instead.

Putting HTML content in scope variables kind of goes against some angular philosophy guidelines, I believe.

If you were to later change the template, would you want it to rebind itself and be processed again?

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.