【问题标题】:Get ID of children Element on click单击时获取子元素的 ID
【发布时间】:2021-03-28 15:11:58
【问题描述】:

我正在尝试获取子组件元素的 id。

<navigation (click)="resetRoute($event)"></navigation

在导航组件中有一个带有 routerLinks 的列表。每个项目都有一个 id。

resetRoute(event) {
  const elementId: string = (event.target as Element).id;
  console.log(elementId);
  console.log(event.srcElement.attributes.id);
  console.log(event.target.attributs.id);
}

它们都返回空白或未定义。我正在使用 Angular v10 和 TypeScript 4.0.5

在我的 routing.module.ts 中:有一个通配符指向另一个组件(例如组件 A)。

我的目标是获取子元素(例如组件 B)的路径并导航到它。

我该怎么做?有没有人解决?

【问题讨论】:

    标签: angular typescript events routes router


    【解决方案1】:

    我已经解决了。

    resetRoute() {
      const url: string = this.router.url.replace('/oldParam', '/newParam');
      this.router.navigateByUrl(url);
    }
    

    新的 url 是 {baseURI]/newParam/{eventParam}。 eventParam 是通过点击功能设置的

    【讨论】:

      猜你喜欢
      • 2012-05-03
      • 1970-01-01
      • 1970-01-01
      • 2019-02-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-11
      相关资源
      最近更新 更多