ホームページ  >  記事  >  ウェブフロントエンド  >  Angular で DOM 要素を操作する方法の詳細な説明

Angular で DOM 要素を操作する方法の詳細な説明

青灯夜游
青灯夜游オリジナル
2022-12-23 19:14:491846ブラウズ

Angular で DOM 要素を正しく操作するにはどうすればよいですか?次の記事では、angular で DOM 要素を操作する方法を紹介します。

Angular で DOM 要素を操作する方法の詳細な説明

DOM 要素を Angular で取得するには、JavaScript のネイティブ API を使用するか、jquery オブジェクトを通じて jQuery を導入します。 Operation DOM ですが、Angular は DOM 要素を取得するための対応する API (ElementRef) を提供しているため、ネイティブ API や jQuery を使用する必要はありません。 [関連チュートリアルの推奨事項: "angular チュートリアル"]

ElementRef DOM 要素を取得します

1. TestComponent# を作成します# #コンポーネント、テンプレートは次のとおりです: test.component.html

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

2.

test.component.ts ファイルを記述します

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;))
    }
}

us

this.el.nativeElement が何であるかを見てみましょう

Angular で DOM 要素を操作する方法の詳細な説明 したがって、
this.el.nativeElement.querySelector( '#component' ) を使用して、対応する DOM 要素を操作します。たとえば、テキストの色

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

テンプレート変数を変更してDOM要素を取得できます

ViewChild##を通じてコン​​ポーネントを取得できます。 #、##ContentChildViewChildren、および ContentChildren# も同様です。

TestComponent
コンポーネントを変更します。

<div>
    <p>你好</p>
</div>
<!-- 1、给元素加入模板变量 div -->
<div #div>
    <span>世界</span>
</div>
<h1>标题</h1>
<!-- 给组件加入模板变量 component -->
<pass-badge #component textColor="red">组件</pass-badge>
2 のように、対応する要素にテンプレート変数を追加します。test.component.ts

を次のように変更します:

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;	// 文字颜色修改为淡蓝色
    }
}
最終結果は次のとおりです

Angular で DOM 要素を操作する方法の詳細な説明結果から、

ViewChild
テンプレート変数を使用してコンポーネント要素を取得すると、取得されるのはエクスポートされたコンポーネント クラスであることがわかります。 (上記の例は

PassBadgeComponent) 現時点では、コンポーネントに含まれるプロパティのみを操作できます。 ViewChild

テンプレート変数を使用して html 要素を取得する場合、取得されるクラスは

ElementRef 型になりますが、このとき this を渡すことができます。 div.nativeElement.querySelector('span') および要素を操作するためのその他のネイティブ APIプログラミング関連の知識の詳細については、

プログラミング チュートリアル
を参照してください。 !

以上がAngular で DOM 要素を操作する方法の詳細な説明の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。