【问题标题】:ng-click won't work with ng-bind-htmlng-click 不适用于 ng-bind-html
【发布时间】:2014-07-16 08:43:45
【问题描述】:

我有这样的html模板:

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

我想使用ng-bind-html绑定这个模板,我尝试使用它,也使用了ng-bind-html-unsafe,但不幸的是它绑定了html字符串,没有点击操作。

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

我读到了类似的问题,它说ng-click 是在ng-bind 之后加载的,那么有人可以向我介绍如何解决这个问题吗?

【问题讨论】:

  • Angular 让做这件事变得如此困难的事实恰恰突出了你不应该这样做的为什么。 HTML 不属于 Controller。通过视图将其拉入
  • 为什么需要在 JS 变量中有模板?
  • 在我正在处理的项目中,我必须将模板绑定到主视图中的子视图,因此我需要将模板与其功能绑定在所需的面板(子视图)中
  • 您使用的是哪个版本的 Angular?

标签: javascript html angularjs ng-bind-html


【解决方案1】:

也许您需要在控制器内部编译模板?

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

【讨论】:

    【解决方案2】:

    您可以尝试 ng-include 并将模板放入静态文件中。

    我相信,将 HTML 内容放在范围变量中有点违反一些角度哲学准则。

    如果您稍后更改模板,您是否希望它重新绑定并再次处理?

    【讨论】:

      猜你喜欢
      • 2014-02-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-17
      • 1970-01-01
      • 1970-01-01
      • 2015-01-18
      • 2023-03-15
      相关资源
      最近更新 更多