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

Angular 4 adding an image before each new line using renderer

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

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 正在为整个添加样式 tooltip.That 很好,我正在进一步尝试的工作是在新行的开头也添加一个图像

编辑

经过多次尝试后,我能够添加图像 "but" 它只是添加到最后一个值而不是以前的值(而我想在每一行添加图像)。

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 标签,然后给它们添加样式。

      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);
    });

另一种布局

你可以试试这样的 -

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 = "Apple,Ball,Cat,Dog,Elephant Elephant Elephant Elephant Elephant Elephant Elephant Elephant Elephant Elephant Elephant ";

这是输出屏幕截图

Test Output