Angular日历标签除了href以外不允许在HTML标签中使用其他属性。
创始人
2024-10-29 05:00:25
0

在Angular中,可以使用Angular指令来解决这个问题。可以创建一个自定义指令来扩展a标签,只允许使用href属性。

首先,在Angular项目中创建一个新的指令文件,命名为hrefOnly.directive.ts

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

@Directive({
  selector: 'a'
})
export class HrefOnlyDirective {
  constructor(private el: ElementRef) { }

  @HostListener('click', ['$event'])
  onClick(event: Event) {
    const target = event.target as HTMLAnchorElement;
    if (!target.href) {
      event.preventDefault();
    }
  }
}

然后,在需要使用这个指令的模块中,将HrefOnlyDirective添加到declarations中:

import { NgModule } from '@angular/core';
import { HrefOnlyDirective } from './hrefOnly.directive';

@NgModule({
  declarations: [
    HrefOnlyDirective
  ],
  exports: [
    HrefOnlyDirective
  ]
})
export class SharedModule { }

现在,可以在模板中使用a标签,并且除了href属性外,不允许使用其他属性:

Link
Link (with target) 

这样,当用户点击带有额外属性的链接时,Angular指令会阻止默认行为,从而确保只有带有href属性的链接可以被点击。

相关内容

热门资讯

透视有挂!wepoker私局辅... 透视有挂!wepoker私局辅助(透视)wepoker有脚本吗,教程指南(有挂讲解)-哔哩哔哩1、下...
wepoker脚本!德州透视h... wepoker脚本!德州透视hhpoker(透视)器-切实解密真的有挂wepoker脚本!德州透视h...
pokemmo辅助工具!德普之... pokemmo辅助工具!德普之星透视辅助软件激活码(透视)挂-原来透视有挂一、德普之星透视辅助软件激...
透视有挂!wepoker买脚本... 透视有挂!wepoker买脚本靠谱吗(透视)约局吧开挂神器是真的吗,教程要领(有挂方略)-哔哩哔哩1...
sohoo辅助!hhpoker... sohoo辅助!hhpoker到底可以透视吗(透视)方法-本来推荐真的是有挂1、许多玩家不知道hhp...
透视辅助!pokemmo手机版... 透视辅助!pokemmo手机版修改器(透视)佛手大菠萝13道挂哪里,教程教材(有挂实锤)-哔哩哔哩佛...
epoker免费透视脚本!hh... epoker免费透视脚本!hhpoker真的假的(透视)app-本来曝光存在有挂1、下载好epoke...
透视开挂!wepokerplu... 透视开挂!wepokerplus万能挂(透视)wpk透视工作室,教程总结(有挂透视)-哔哩哔哩1.w...
wpk辅助插件!wepoker... wpk辅助插件!wepoker辅助透视软件(透视)神器-真是辅助真的是有挂运wepoker辅助透视软...
透视曝光!hhpoker是真的... 透视曝光!hhpoker是真的吗(透视)hhpoker辅助挂,教程练习(有挂工具)-哔哩哔哩1)hh...