Angular 4使用渲染器在每行之前添加图像

时间:2019-01-17 09:15:12

标签: javascript angular html5 dom angular-renderer2

我正在尝试在每行之前添加图片 所以我能够做到这一点

Apple
Ball
Cat
Dog

但是我想在每行之前添加图片

(img) Apple
(img) Ball
(img) Cat
(img) Dog

代码

this.tooltipTitle = "Apple,Ball,Cat,Dog,Elephant"

create() {
  this.tooltip = this.renderer.createElement('div');
  this.tooltipTitle.split(',').forEach((text) => {
  this.renderer.appendChild(this.tooltip, this.renderer.createText(text));
  this.renderer.appendChild(this.tooltip, this.renderer.createElement('br'));
  this.renderer.appendChild(document.body, this.tooltip);
 });
}

this.renderer.addClass(this.tooltip,'classA')

 .classA{
   positon:absolute;
   max-width: 150px;
   font-size: 14px;
   color: black;
   padding: 3px 8px;
   background: grey;
   border-radius: 4px;
   z-index: 100;
   opacity: 0;
 }

所以addClass会在整个工具提示中添加样式。这很好,我进一步尝试的工作是在新行的开头也添加图片

编辑

尝试了很多之后,我就可以添加图像了,但是它只是被添加到最后一个值上,而不是以前的值上(而我想在每行上添加图像),我们将不胜感激,请引导我

this.tooltip = this.renderer.createElement('div');
this.imgforres = this.renderer.createElement('img');
this.imgsrc = 
this.renderer.setAttribute(this.imgforres,"src",
"assets/images/restriction.png")

this.tooltipTitle.split(',').forEach((text) => {
this.renderer.appendChild(this.tooltip,this.imgforres);
this.renderer.appendChild(this.tooltip, this.renderer.createText(text));
this.renderer.appendChild(this.tooltip, this.renderer.createElement('br'));
this.renderer.appendChild(document.body, this.tooltip);
});

最新

现在我已经实现了在每行新文本之前获得图像,感谢Yuri的回复 最后一个小问题是,如果文本很长一定会自动换行,但缩进不是上面的行文本,那么缩进会从图像下方开始

<img> Apple
<img> Ball
<img> So this a long text
and it starts from below 
the image
<img> Cat

预期

<img> Apple
<img> Ball
<img> So this a long text
      and it starts from 
      below the image
<img> Cat

我已经尝试过断字,辩解,这无济于事,也许我必须将此文本嵌入div或p标签中,然后为其指定样式。请提出建议。

2 个答案:

答案 0 :(得分:2)

      this.tooltip = this.renderer.createElement('div');
      this.tooltipTitle.split(',').forEach((text) => {
      this.renderer.appendChild(this.tooltip, this.renderer.createText(text));
      this.renderer.appendChild(this.tooltip, this.renderer.createElement('br'));

      var img2 = document.createElement('img'); // Use DOM HTMLImageElement
      img2.src = 'image2.jpg';
      img2.alt = 'alt text';
      this.renderer.appendChild(this.tooltip,img2);
      this.renderer.appendChild(this.tooltip, this.renderer.createElement('br'));

      this.renderer.appendChild(document.body, this.tooltip);
    });

enter image description here

另一种布局 enter image description here

答案 1 :(得分:0)

您可以尝试这样的事情-

create(){
  this.tooltip = this.renderer.createElement('div');

    this.tooltipTitle.split(',').forEach((text) => {
      let container = this.renderer.createElement('div');
      let img = this.renderer.createElement('img');
      this.renderer.setAttribute(img, "src", "assets/images/restriction.png");
      this.renderer.appendChild(container, img);

      let textSpan = this.renderer.createElement('span');
      this.renderer.appendChild(textSpan, this.renderer.createText(text));
      this.renderer.appendChild(container, textSpan);

      this.renderer.appendChild(this.tooltip, container);
    });

    this.renderer.appendChild(document.body, this.tooltip);
}

在html中,我保留了一个内联CSS,您可以将其放在单独的文件中,并通过调用this.renderer.addClass()

加载它
<style>
  span { 
    margin-left: 10px;
    display:inline-flex;
    width:150px;
    word-wrap:break-word;
} 
</style>

这是我的测试工具提示标题-

tooltipTitle =“苹果,球,猫,狗,大象大象大象大象大象大象大象大象大象大象大象”“

这是输出屏幕截图

Test Output