简体   繁体   English

在新选项卡中打开由[innerHTML]绑定的Angular 6链接

[英]Opening Angular 6 links bound by [innerHTML] in a new tab

I have an Angular 6 app with a <div> that gets populated via an [innerHTML] binding. 我有一个带有<div>的Angular 6应用程序,它通过[innerHTML]绑定填充。 How can I apply target='_blank' to all the links inside this div ? 如何将target='_blank'应用于此div所有链接?

What I've tried: 我尝试过的:

So far, I've tried creating a directive that wraps the div and, after change detection gets run, pulls up a list of child <a> tags and applies a target='_blank' attribute. 到目前为止,我已经尝试创建一个包装div的指令,并在更改检测运行后,提取一个子<a>标签列表并应用target='_blank'属性。 So far, I have not been able to get ContentChildren to access any of the links: It just pulls up an empty list. 到目前为止,我还没有能够让ContentChildren访问任何链接:它只是提取一个空列表。

Does anyone have experience doing this, or is there a more elegant solution? 有没有人有这方面的经验,还是有更优雅的解决方案?

@Directive({
  selector: '[appExternalLink]'
})
export class ExternalLinkDirective implements AfterContentChecked, AfterViewChecked {

  @ContentChildren('a', {descendants: true}) links: QueryList<any>;


  @Input() appExternalLink: string;
  constructor() {
    console.log('HELLO FROM APPEXTERNALLINK');
  }

  ngAfterContentChecked() {
    console.log(this.links);
  }

}

Then, when binding the content: 然后,绑定内容时:

<div appExternalLink>
  <div [innerHTML]="content"></div>
</div>

You can use the <base> tag to default the target attribute of each anchor. 您可以使用<base>标记来默认每个锚点的target属性。

<base target="_blank">

You can inject this tag dynamically in ngOnInit and remove it in ngOnDestroy , but it will change the behaviour of any link. 您可以动态地注入这个标签ngOnInit并删除它ngOnDestroy ,但它会改变任何链接的行为。

If you want to change the behaviour of just the anchors inside a div, you can use 如果你想改变div中的锚点的行为,你可以使用

Array.from(document.querySelectorAll('.your-div-class a'))
  .forEach(el => el.setAttribute('target', '_blank'))

It turns out I was using the wrong approach trying to use ContentChildren . 事实证明我使用错误的方法尝试使用ContentChildren

With the below code, I was able to target all <a> tags in a div with the appExternalLink directive. 使用下面的代码,我能够使用appExternalLink指令定位div中的所有 <a>标签。 Posting my solution so other people don't have to figure this out themselves: 发布我的解决方案,以便其他人不必自己解决这个问题:

  import { AfterViewChecked, Directive, ElementRef } from '@angular/core';

  @Directive({
    selector: '[appExternalLink]'
  })
  export class ExternalLinkDirective implements AfterViewChecked {
    constructor(private el: ElementRef) { }

    ngAfterViewChecked() {
      Array.from(this.el.nativeElement.querySelectorAll('a'))
        .forEach((el: any) => {
          el.setAttribute('target', '_blank');
        });
    }
  }

This is the selector you're using 这是您正在使用的选择器

@ContentChildren('a', {descendants: true}) links: QueryList<any>;

What this does is look for any direct children of the component which have an id of a . 这样做是为了找到id为a的组件的任何直接子节点。

If you change your html to look like this: 如果您将html更改为如下所示:

<div appExternalLink>
    <a #a>Link</>
    <a #a>Link</>
</div>

Then your selector will find the links. 然后您的选择器将找到链接。

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

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