angularjs - ng-bind-html 中的 Angular 指令未被评估

标签 angularjs ng-bind-html

这是 this post 的后续问题.

我在使用 ng-bind-html 注入(inject)的 html 中使用了一个 ng-attr-title,它不起作用,即)标题未在 DOM 中形成因此悬停在 tooltip 上的元素未形成。这是我的代码

myApp.controller("MyCtrl",function($scope) {
$scope.tl="this is title";
$scope.level = "<span ng-attr-title='{{tl}}'><b>data</b></span>";
});

问题在 Jsfiddle 中说明。

最佳答案

您必须使用 $compile 服务来实现这一点。

JS:

var myApp = angular.module('myApp', ['ngSanitize']); 

myApp.controller("MyCtrl", function($scope){
    $scope.tl="this is title";
    $scope.level = "<span ng-attr-title='{{tl}}'><b>data</b></span>"; 
});

myApp.directive('compileHtml', compileHtml);

function compileHtml($compile) {
    return {
        restrict: 'A',
        link: function (scope, element, attrs) {
            scope.$watch(function () {
                return scope.$eval(attrs.compileHtml);
            }, function (value) {
                element.html(value);
                $compile(element.contents())(scope);
            });
        }
    };
}

HTML:

<div ng-controller="MyCtrl" id="tableForVxp" class="dataDisplay2">
  <span compile-html="level" ></span>
</div>

compileHtml 指令将针对您的 $scope 编译您的 HTML 模板。

关于angularjs - ng-bind-html 中的 Angular 指令未被评估,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31722343/

相关文章:

python - 使用 selenium python 按类名查找第 n 个元素

javascript - ng-repeat 内的 NG-Bind-Html

angularjs - 使用 ng-bind-html 和 $sce.trustAsHtml 创建一个带有 ng-model 绑定(bind)的字符串

javascript - ng-bind-html 添加了拥有的 css 类 'ng-binding',它破坏了所有嵌套的 css,如何摆脱它?

AngularJS:是否可以在没有 $watch 的情况下对 ng-bind-html 进行后处理?

html - 带有 ng-repeat 和 ng-bind-html 的 Accordion 将不起作用

javascript - 如何在 AngularJS 中同时处理 DOM 操作和状态更改?

javascript - Angular js 简单 ng-include 不起作用

angularjs - 将node js集成到cordova移动应用程序中(将移动应用程序连接到服务器)

javascript - 在 Javascript 函数中传递变量