简体   繁体   English

将内容添加到uib-tooltip-html

[英]add content to uib-tooltip-html

Why does this work just fine: 为什么这样可以正常工作:

Controller: 控制器:

$scope.getHtml = $sce.trustAsHtml("<div>Test</div>");

View: 视图:

<div uib-tooltip-html="getHtml">Test</div>

But the following doesn't work at all? 但是以下内容根本行不通吗? ;-( ;-(

Controller: 控制器:

$scope.getHtml = function (){
        var textOutput = "<div>Text</div>";
        return $sce.trustAsHtml(textOutput);        
    };

View: 视图:

<div uib-tooltip-html="getHtml()">Test</div>

Error message is: angular.min.js:6 Uncaught Error: [$rootScope:infdig] 错误消息是:angular.min.js:6未捕获的错误:[$ rootScope:infdig]


Edit 1: Thank you, original problem solved. 编辑1:谢谢,原来的问题解决了。 But now I have another question on this one. 但是现在我对此有另一个疑问。 What if the <div> is within a ngRepeat and I want to give the function an item of ngRepeat as an argument? 如果<div>在ngRepeat中并且我想给函数一个ngRepeat项作为参数怎么办? Like <div uib-tooltip-html="getHtml({{item}})">Test</div> ? <div uib-tooltip-html="getHtml({{item}})">Test</div>


Edit 2: Here is a more specific example of my problem. 编辑2:这是我的问题的更具体的示例。 I want something like this to work: 我希望这样的工作:

<tr ng-repeat="object in objectArray">
    <td uib-tooltip-html="getHtml(object.value1)">{{object.value1}}<td>
    <td uib-tooltip-html="getHtml(object.value2)">{{object.value2}}<td>
    <td uib-tooltip-html="getHtml(object.value3)">{{object.value3}}<td>
</tr>

with

$scope.getHtml = function (value) {
    var textOutput = doSomethingWithObjectDataAndCreateHtmlFromIt(value);
    return $sce.trustAsHtml(textOutput);        
}

But it doesn't work. 但这是行不通的。 When I use uib-tooltip instead of uib-tooltip-html I can see the tooltip with the correct html (unparsed) in it, but if I change it to uib-tooltip-html there's the error again. 当我使用uib-tooltip而不是uib-tooltip-html我可以看到其中带有正确html(未解析)的工具提示,但是如果将其更改为uib-tooltip-htmluib-tooltip-html再次出现错误。

In the first case $scope.getHtml ends up to be a string . 在第一种情况下, $scope.getHtml最终是一个string

In the second case $scope.getHtml ends up to be a function , which returns the same string as the first case. 第二种情况下, $scope.getHtml最终是一个function ,它返回与第一种情况相同的字符串。

However if you call your function in the second case, it would work, as it is essentially the same code: 但是,如果您在第二种情况下调用函数,则它将起作用,因为它实质上是相同的代码:

$scope.getHtml = function () {
    var textOutput = "<div>Text</div>";
    return $sce.trustAsHtml(textOutput);        
} ()

if you want to pass the html data in ng-repeat, just do this: 如果要在ng-repeat中传递html数据,请执行以下操作:

$scope.getHtml = function (html) {
    return $sce.trustAsHtml(html);        
}

and use it by: 并通过以下方式使用它:

<div uib-tooltip-html="getHtml()">Test</div>

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

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