简体   繁体   English

ng-click不适用于ng-bind-html

[英]ng-click won't work with ng-bind-html

I have html template like this: 我有像这样的html模板:

$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. 我想使用ng-bind-html绑定这个模板,我试图使用它,我也使用了ng-bind-html-unsafe ,但不幸的是它绑定了html字符串,因为没有点击动作。

<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? 我读到了类似的问题,它说ng-click是在ng-bind之后加载的,所以有人可以向我介绍如何解决这个问题吗?

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);
});

You could try ng-include and put your template in to a static file instead. 您可以尝试使用ng-include并将模板放入静态文件中。

Putting HTML content in scope variables kind of goes against some angular philosophy guidelines, I believe. 我认为,将HTML内容放在范围变量中会违背一些有棱角的哲学准则。

If you were to later change the template, would you want it to rebind itself and be processed again? 如果您以后要更改模板,是否要重新绑定并重新处理?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM