AngularJs:抓取编译的 html 并设置为工具提示 [英] AngularJs: grab compiled html and set into tooltip
问题描述
我正在使用 angular js bootstrap 工具提示来显示一组元素上的工具提示.
I'm using angular js bootstrap tooltip to show tooltips on a set of elements.
Plunker:http://plnkr.co/edit/9xk41f3CR0wnajN71bSi
我需要注入由 angular 编译的工具提示 html,但我真的不知道如何.工具提示教程对我没有用,因为它从作用域中获取 html 作为变量,但对于一组元素,这是不可能的.
I need to inject into the tooltip html compiled by angular, but i don't really get how. The tooltip tutorial is not useful to me because it gets the html from the scope as variable, but for a set of elements this is not possible.
如何填写tooltip-html-unsafe
?
推荐答案
你可以这样做:
HTML:
<li ng-repeat="phone in phones">
<div phone-info index="{{$index}}">
<p tooltip-html-unsafe="{{tooltips[$index] }}">A tooltip should appear on top of this line ({{ phone.name }} - {{ phone.snippet }})</p>
<div>
</li>
添加到控制器:
$scope.tooltips = [];
指令:
app.directive('phoneInfo', function($compile, $timeout) {
/* wrap in root element so we can get final innerHTML*/
var tipTemplate = '<div><p> This will be the content of {{phone.name}} injected in the tooltip </p><div>';
return {
link: function(scope, el, attrs) {
var tipComp = $compile(tipTemplate)(scope)
$timeout(function() {
scope.tooltips[attrs.index] = tipComp.html()
});
}
}
})
使用 index
来避免创建孤立的作用域.也可以使用隔离范围完成并创建 phone
的属性而不是使用 scope.tooltips[index]
Used index
to avoid creating an isolated scope. Can also be done with isolated scope and create a property of phone
instead of using scope.tooltips[index]
这篇关于AngularJs:抓取编译的 html 并设置为工具提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!