【问题标题】:Check for URL's in array and iterate based on conditions in angular检查数组中的 URL 并根据角度条件进行迭代
【发布时间】:2021-12-06 09:24:06
【问题描述】:

我正在处理一个 Angular 项目,我在 API 的响应中获得了 3 种类型的链接,如下所示,我正在 html 中通过数组对其进行迭代。

case 1: <a class="test" href="https://www.google.com/" target="_blank">click here</a> ,
case 2: 'with only href ------- href="https://www.facebook.com/". ',
case 3: 'Without a tag and href ----- "https://www.google.com/"',

我想在我的输出中显示href 作为超链接作为响应,它应该导航到相应的链接。 为此,我使用了[innerHTML]="result",它适用于案例 1。

到目前为止,它在输出中显示为案例 2 和案例 3 的纯文本。 我使用正则表达式将它们转换为链接。 如果我将它们放在单独的变量中,它工作正常。

如何使用我在遍历数组时已经完成的正则表达式的条件。

请帮我实现这个功能。

我在 stackblitz 中更新了我的代码。

工作堆栈闪电战是:

https://stackblitz.com/edit/angular-ivy-ckabj3?file=src%2Fapp%2Fapp.component.html

来自 API 的响应: 可以有任意数量的任意条件的对象,而不仅仅是低于 3 个。

  apiresponse: any = {
    response: [
      {
        hrefmessages:
          'with only href ------- href="https://www.facebook.com/". ',
      },

      {
        hrefmessages: 'Without a tag and href ----- "https://www.google.com/"',
      },
      {
        hrefmessages:
          'with both a tag and href ------- <a class="test" href="https://www.google.com/" target="_blank">click here</a>. ',
      },
    ],
  };

HTML:

<div *ngFor="let result of hrefArray">
  <p
    [innerHTML]="result"
  ></p>
</div>

TS:

  withHrefAndWithoutAtag() {
    let match = this.withJustHref.match(
      /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gi
    );
    console.log('match', match);
    let final = this.withJustHref.replace('href=', '');
    console.log('final', final);
    match.map((url) => {
      final = final.replace(
        url,
        '<a href="' + url + '" target="_BLANK">' + url + '</a>'
      );
    });
    console.log(final);
    this.withJustHref = final;

    return final;
  }

【问题讨论】:

    标签: javascript arrays angular typescript forms


    【解决方案1】:

    主要变化是让您的方法将字符串作为输入,而不是从全局变量中读取。

    这就是结果。基本上我先检查类别,然后调用你已经编写的正确的 map 方法。

    app.component.ts

    import { Component, OnInit } from '@angular/core';
    
    @Component({
      selector: 'my-app',
      templateUrl: './app.component.html',
      styleUrls: ['./app.component.css'],
    })
    export class AppComponent implements OnInit {
      hrefArray: any = [];
    
      apiresponse: any = {
        response: [
          {
            hrefmessages:
              'with only href ------- href="https://www.facebook.com/". ',
          },
    
          {
            hrefmessages: 'Without a tag and href ----- "https://www.google.com/"',
          },
          {
            hrefmessages:
              'with both a tag and href ------- <a class="test" href="https://www.google.com/" target="_blank">click here</a>. ',
          },
        ],
      };
    
      ngOnInit(): void {
        this.hrefArray = this.mapHrefs();
      }
    
      mapHrefs(): string[] {
        return this.apiresponse.response.map((item) => {
          let message = item.hrefmessages;
          if (message.includes('<a ')) return message;
    
          if (message.includes('href')) return this.withHrefAndWithoutAtag(message);
    
          return this.withoutHrefAndAtag(message);
        });
      }
    
      withHrefAndWithoutAtag(item: string): string {
        let match = item.match(
          /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gi
        );
        console.log('match', match);
        let final = item.replace('href=', '');
        console.log('final', final);
        match.map((url) => {
          final = final.replace(
            url,
            '<a href="' + url + '" target="_BLANK">' + url + '</a>'
          );
        });
        console.log(final);
    
        return final;
      }
    
      withoutHrefAndAtag(item: string): string {
        let match = item.match(
          /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gi
        );
        let final = item;
        match.map((url) => {
          final = final.replace(
            url,
            '<a href="' + url + '" target="_BLANK">' + url + '</a>'
          );
        });
    
        return final;
      }
    }
    

    app.component.html

    <div  *ngFor="let result of hrefArray">
      <p [innerHTML]="result"></p>
    </div>
    

    【讨论】:

    • 您好,先生,感谢您的回复。这是工作。您能否澄清一下,如何在输入类型“按钮”上进行相同的迭代??
    • 一旦你有了&lt;div *ngFor="let result of hrefArray"&gt;[Any html code]&lt;/div&gt;,你可以放任何你想要的东西。在 div 元素中,您有变量 result 即您的 URL,您可以将它放在一个按钮中 :)
    • 实际上我的疑问是,如果我的响应包含数组中的数组怎么办,那么上述解决方案不起作用。请帮我。 apiresponse: any = {response: [{hrefmessages:'google.com" target="_blank">点击这里',},{hrefmessages:' google.com ',},{hrefmessages:'',buttonsText:[{title:'with only href="facebook.com/".',},{title:'"google.com"',},{title:'with both一个标签和一个 href -- google.com" target="_blank">点击这里',},{title: '在一个标签之间 -- "google.com/"<a>' ,},],},],};
    猜你喜欢
    • 1970-01-01
    • 2022-11-17
    • 1970-01-01
    • 2018-07-16
    • 1970-01-01
    • 2023-02-20
    • 2019-07-08
    • 2019-03-13
    • 2018-07-27
    相关资源
    最近更新 更多