【问题标题】:AngularJS Display Tooltip Content of Array of Strings / With HTMLAngularJS显示字符串数组的工具提示内容/使用HTML
【发布时间】:2016-03-03 02:55:04
【问题描述】:

HTML(翡翠):

a(href='#', data-placement='right', title="{{ someArrayOfStrings }}" tooltip)

someArrayOfStrings 在我的角度控制器的初始化过程中被声明为一个值:

角度:

var myController = function($scope) {
    $scope.initialize = function(someArrayOfStrings) {
        $scope.someArrayOfStrings = someArrayOfStrings;
    }
};

var tooltip = function() {
    return {
        restrict: 'A',
        link: function(scope, element, attrs){
            $(element).hover(function(){
                $(element).tooltip('show');
            }, function(){
                $(element).tooltip('hide');
            });
        }
    };
};

目前,这会给我的工具提示内容如下:

["string1","string2","string3"]

这太丑了,完全不是我想要的。

我想要显示的是这样的:

string1
string2
string3

我已经四处搜索,似乎有很多方法可以解决这个问题,但到目前为止,我还没有设法让它很好地工作,例如http://jsfiddle.net/superscript18/fu7a2/6/http://jsfiddle.net/WR76R/Render Html String in tooltip

或者,我也许可以使用由<br/> 标记分隔的串联字符串。但是,这将要求我的工具提示接受 HTML 标记。似乎有一个可用的工具提示选项,即html: true,但我无法让它工作。

想法/帮助?


演示小提琴@jsfiddle.net/zr89sq6L/6/

【问题讨论】:

  • 用于指令.tooltip - 工具提示是jquery?
  • 它是有角的;见stackoverflow.com/questions/20666900/…,@StepanKasyanenko
  • 你在尝试$(element).tooltip({html: 'true', container: 'body'})吗?
  • 也许最好使用用于angularbootstrap?看到这个angular-strap
  • 你能创建一个jsFiddle吗?

标签: jquery angularjs twitter-bootstrap pug


【解决方案1】:

试试这个解决方案jsfiddle

  • 首先,创建 directive 以创建 tooltip $(element).tooltip({html: 'true'})

  • 其次,使用stringArray.join('<br>')br创建字符串。

例子:

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

myApp.controller('MyCtrl', function($scope) {
    $scope.name = 'Superhero';
    $scope.stringArray = ["string1", "string2", "string3"];

  })
  .directive('tooltip', function() {
    return {
      restrict: "A",
      link: function(scope, element) {
        $(element).tooltip({
          html: 'true'
        });
      }
    }
  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.9.0/jquery-ui.js"></script>
<link href="http://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css" rel="stylesheet" type="text/css" />
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular.min.js"></script>
<div ng-app="myApp">
  <div ng-controller="MyCtrl">
    Hello, {{name}}!
    <div ng-repeat="string in stringArray">
      {{ string }}
    </div>
    <a href="#" data-placement="right" title="{{ stringArray.join('<br>') }}" tooltip>tooltip</a>
  </div>
</div>

【讨论】:

  • 它适用于小提琴,但不适用于我的 webapp,:( 我的字符串看起来像这样:string1&lt;br&gt;string2&lt;br&gt;string3。它仍然没有呈现 HTML。
  • 我意识到这很有趣。 .run 正在我的 web 应用程序的左上角出现一个奇怪的工具提示 div,它显示我的页面标题(不是“string1”等)。此外,我不知道它何时出现和消失。看起来非常奇怪和随意。它还更改了我的浏览器选项卡 O_O 上的标题。知道为什么吗?我的工具提示在正文 > 容器 > 一些行和列 > 面板正文内。
  • 嘿,成功了!奇怪的!因为我以为我以前尝试过,但它没有用:S 也许我当时打错了 T_T 非常感谢! :) @Stepan Kasyanenko
【解决方案2】:

你只需要像title="{{ stringArray.join('\u000A') }}"这样设置title属性。请检查小提琴http://jsfiddle.net/Noblemo/u9tt9xb2/

【讨论】:

  • 它适用于小提琴,但不适用于我的 web 应用程序,:( 我的字符串看起来像是用空格而不是换行符分隔/连接,例如 string1 string2 string3
  • 仅供参考,我正在使用 Firefox:/ 但它仍然没有显示为新行。
  • firefox是哪个版本的?
  • 你能试试用 \n 或 \x0A 代替 \u000A 吗?
  • 我都试过了,但都不起作用,:S @NOBLE M.O. Ubuntu 14.04 上的版本 44.02。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多