Home > Web Front-end > JS Tutorial > An article to talk about how to operate DOM elements in Angular

An article to talk about how to operate DOM elements in Angular

青灯夜游
Release: 2022-01-22 09:58:32
forward
2542 people have browsed it

How to operate DOM elements in Angular? The following article will introduce to you how to operate DOM elements in angular. I hope it will be helpful to you!

An article to talk about how to operate DOM elements in Angular

To obtain DOM elements in angular, you can use javascript's native API, or introduce jQuery to operate the DOM through jquery object , but angular has provided us with the corresponding API (ElementRef) to obtain DOM elements, so there is no need to use the native API or jQuery. [Related tutorial recommendations: "angular tutorial"]

ElementRef Get the DOM element

1. Create TestComponentComponent, the template is as follows: test.component.html

<div>
	<p>你好</p>
</div>
<div>
    <span>世界</span>
</div>
<h1>标题</h1>
<pass-badge id="component" textColor="red">组件</pass-badge>
Copy after login

2. Write the test.component.ts file

import { Component, OnInit } from &#39;@angular/core&#39;;
// 1、导入 ElementRef 类
import { ElementRef} from &#39;@angular/core&#39;;
import { PassBadge } from &#39;./compoment/pass-badge/pass-badge.component&#39;

@Component({
  selector: &#39;app-test&#39;,
  templateUrl: &#39;./test.component.html&#39;,
  styleUrls: [&#39;./test.component.css&#39;],
  declarations: [ PassBadge ]
})
export class TestComponent implements OnInit {
	// 2、将 ElementRef 类注入 test 组件中
    constructor(private el:ElementRef) {}

    ngOnInit() {
    	// 3、获取 DOM 元素
        console.log(this.el.nativeElement)
        console.log(this.el.nativeElement.querySelector(&#39;#component&#39;))
    }
}
Copy after login

us Let’s see what this.el.nativeElement is

An article to talk about how to operate DOM elements in Angular
So you can pass this.el.nativeElement.querySelector('#component' ) to operate the corresponding DOM element. For example, you can change the text color

this.el.nativeElement.querySelector(&#39;#component&#39;).style.color = &#39;lightblue&#39;
Copy after login

Template variable to get the DOM element

You can get the component throughViewChild, and the same isContentChild, ViewChildren and ContentChildren

1. Modify the TestComponent component and add template variables to the corresponding elements. , as follows

<div>
    <p>你好</p>
</div>
<!-- 1、给元素加入模板变量 div -->
<div #div>
    <span>世界</span>
</div>
<h1>标题</h1>
<!-- 给组件加入模板变量 component -->
<pass-badge #component textColor="red">组件</pass-badge>
Copy after login

2. Modify test.component.ts as follows:

import { Component, OnInit } from &#39;@angular/core&#39;;
import { ElementRef} from &#39;@angular/core&#39;;
// 2、引入ViewChild
import { ViewChild } from &#39;@angular/core&#39;

@Component({
  selector: &#39;app-test&#39;,
  templateUrl: &#39;./test.component.html&#39;,
  styleUrls: [&#39;./test.component.css&#39;]
})
export class TestComponent implements OnInit {
    constructor(private el:ElementRef) {}
    // 3、获取元素
    @ViewChild(&#39;component&#39;) dom: any;
    @ViewChild(&#39;div&#39;) div: any;
    ngOnInit() {
        console.log(this.dom)	// PassBadgeComponent
        this.dom.fn()   // 调用 passbadge 组件的 fn 方法
        console.log(this.div)	// ElementRef
        this.div.nativeElement.style.color = &#39;lightblue&#39;	// 文字颜色修改为淡蓝色
    }
}
Copy after login

The final result is as follows

An article to talk about how to operate DOM elements in Angular

We can know from the results that when using the ViewChild template variable to obtain the component element, what is obtained is the component class exported by the component (the above example is PassBadgeComponent). At this time Only properties contained in the component can be manipulated.

When using the ViewChild template variable to obtain the html element, the class obtained is the ElementRef type. At this time, you can pass this.div.nativeElement. querySelector('span') and other native APIs to operate elements

For more programming-related knowledge, please visit:Programming Video! !

The above is the detailed content of An article to talk about how to operate DOM elements in Angular. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template