迹忆客 专注技术分享

当前位置:主页 > 学无止境 > WEB前端 > Angular >

Angular 中的 getElementById 替代

作者:迹忆客 最近更新:2023/03/23 浏览次数:

我们将介绍 Angular 中 getElementById 函数的替代。

我们还将在 Angular 中引入 ElementRef 函数以从输入中获取文档的元素引用和值。


Angular 中的 getElementById 替代

getElementById 方法返回具有指定值的 ID 属性的元素。这种方法在 HTML DOM 中最常见,几乎每次我们操作、获取信息或文档中的元素时都会用到。

因为 getElementByIdvanilla Javascript 函数并且我们在 Angular 中使用 Typescript,所以 Typescript 中这个函数的替代是 ElementRef,我们将在本教程中介绍。


Angular 中的 ElementRef

ElementRef 是一个类,它可以包裹指定的 DOM 元素以增强本机元素的方法和属性。如果我们将元素定义为 ElementRef,我们可以将其作为 nativeElement 对象访问。

本文与 Angular 4 到 Angular 12 兼容。

让我们在带有@ViewChild 装饰器的类中使用 ElementRef Interface 来获取元素引用。

首先,我们将编辑我们的 app.component.html 模板并向其中添加以下代码。

# angular

<div #myNameElem></div>

<button (click)="getValue()">Get Value</button>

输出:

ElementRef Angular 的第一个示例

app.component.ts 文件中,我们将添加以下内容。

#angular
import { Component, VERSION, ViewChild, ElementRef } from "@angular/core";
@Component({
  selector: "my-app",
  templateUrl: "./app.component.html",
  styleUrls: ["./app.component.css"]
})

export class AppComponent {
  name = "Angular " + VERSION.major;
  @ViewChild("myNameElem") myNameElem: ElementRef;
  getValue() {
    console.log(this.myNameElem);
    this.myNameElem.nativeElement.innerHTML = "I am changed by ElementRef & ViewChild";
  }
}

输出:

Angular 中的元素引用


在 Angular 中使用 ElementRef 从输入中获取值

现在我们将介绍如何在 Angular 中使用 ElementRef 获取输入的值。

首先,我们将编辑我们的 app.component.html 模板并向其中添加以下代码。

# angular
<input type="text" name="name" #myNameElem />
<button (click)="getValue()">Get Value</button>
<button (click)="resetValue()">Reset</button>

输出:

Angular 中 ElementRef 的第二个示例

现在,在 app.component.ts 文件中,我们将添加以下内容:

#angular
import { Component, VERSION, ViewChild, ElementRef } from '@angular/core';
@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css'],
})
export class AppComponent {
  name = 'Angular ' + VERSION.major;
  @ViewChild('myNameElem') myNameElem: ElementRef;
  getValue() {
    console.log(this.myNameElem.nativeElement.value);
  }
  resetValue() {
    this.myNameElem.nativeElement.value = '';
  }
}

输出:

Angular 中的 ElementRef 示例

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

Do you understand JavaScript closures?

发布时间:2025/02/21 浏览次数:108 分类:JavaScript

The function of a closure can be inferred from its name, suggesting that it is related to the concept of scope. A closure itself is a core concept in JavaScript, and being a core concept, it is naturally also a difficult one.

Do you know about the hidden traps in variables in JavaScript?

发布时间:2025/02/21 浏览次数:178 分类:JavaScript

Whether you're just starting to learn JavaScript or have been using it for a long time, I believe you'll encounter some traps related to JavaScript variable scope. The goal is to identify these traps before you fall into them, in order to av

How much do you know about the Prototype Chain?

发布时间:2025/02/21 浏览次数:150 分类:JavaScript

The prototype chain can be considered one of the core features of JavaScript, and certainly one of its more challenging aspects. If you've learned other object-oriented programming languages, you may find it somewhat confusing when you start

用 jQuery 检查复选框是否被选中

发布时间:2024/03/24 浏览次数:102 分类:JavaScript

在本教程中学习 jQuery 检查复选框是否被选中的所有很酷的方法。我们展示了使用直接 DOM 操作、提取 JavaScript 属性的 jQuery 方法以及使用 jQuery 选择器的不同方法。你还将找到许多有用的

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便