最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

在 Angular 中使用 ViewChild 訪問子組件、指令或 DOM 元素的操作方法

 更新時間:2024年08月12日 09:53:12   作者:白如意i  
這篇文章主要介紹了如何在 Angular 中使用 ViewChild 來訪問子組件、指令或 DOM 元素,在本教程中,您使用了 ViewChild 來從父組件類中訪問指令、子組件和 DOM 元素,需要的朋友可以參考下

簡介

本文將向您介紹 Angular 的 ViewChild 裝飾器。

在某些情況下,您可能希望從父組件類中訪問指令、子組件或 DOM 元素。ViewChild 裝飾器返回與給定指令、組件或模板引用選擇器匹配的第一個元素。

先決條件

如果您想要跟隨本教程進行操作:

  • 考慮安裝 @angular/cli。
  • 使用 @angular/cli 創(chuàng)建一個新項目,以測試 ViewChild 在其中的功能。

本教程已經(jīng)驗證過可以在 @angular/core v13.0.2 和 @angular/cli v13.0.3 下使用。

使用 ViewChild 與指令

ViewChild 使得訪問指令成為可能。

假設(shè)您有一個 SharkDirective。該指令將查找具有屬性 appShark 的元素,并在元素的文本前面添加單詞 "Shark"。

理想情況下,您將使用 @angular/cligenerate 您的指令:

ng generate directive shark --skip-tests

此命令將創(chuàng)建一個 shark.directive.ts 文件,并將該指令添加到 app.module.ts

import { SharkDirective } from './shark.directive';
...
@NgModule({
  declarations: [
    AppComponent,
    SharkDirective
  ],
  ...
})

然后,使用 ElementRefRenderer2 來重寫文本。將 shark.directive.ts 的內(nèi)容替換為以下內(nèi)容:

import {
  Directive,
  ElementRef,
  Renderer2
} from '@angular/core';
@Directive(
  { selector: '[appShark]' }
)
export class SharkDirective {
  creature = 'Dolphin';
  constructor(elem: ElementRef, renderer: Renderer2) {
    let shark = renderer.createText('Shark ');
    renderer.appendChild(elem.nativeElement, shark);
  }
}

接下來,在組件模板中的一個包含文本的 span 中添加一個 appShark 屬性。將 app.component.html 的內(nèi)容替換為以下內(nèi)容:

<span appShark>Fin!</span>

在瀏覽器中查看應用程序時,將在元素的內(nèi)容之前呈現(xiàn)單詞 "Shark"

Shark Fin!

現(xiàn)在,您還可以訪問 SharkDirectivecreature 實例變量,并使用其值設(shè)置一個 extraCreature 實例變量。將 app.component.ts 的內(nèi)容替換為以下內(nèi)容:

import {
  Component,
  ViewChild,
  AfterViewInit
} from '@angular/core';
import { SharkDirective } from './shark.directive';
@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements AfterViewInit {
  extraCreature!: string;
  @ViewChild(SharkDirective)
  set appShark(directive: SharkDirective) {
    this.extraCreature = directive.creature;
  };
  ngAfterViewInit() {
    console.log(this.extraCreature); // Dolphin
  }
}

此代碼使用了一個 setter 來設(shè)置 extraCreature 變量。請注意,它等待 AfterViewInit 生命周期鉤子來訪問變量,因為這是子組件和指令可用的時候。

在瀏覽器中查看應用程序時,您仍將看到 "Shark Fin!" 消息。但是,在控制臺日志中,它將顯示:

Dolphin

父組件能夠訪問指令的值。

使用 ViewChild 與 DOM 元素

ViewChild 使得訪問具有模板引用變量的本機 DOM 元素成為可能。

假設(shè)您在模板中有一個帶有 #someInput 引用變量的 <input>。將 app.component.html 的內(nèi)容替換為以下內(nèi)容:

<input #someInput placeholder="Your favorite sea creature">

現(xiàn)在,您可以使用 ViewChild 訪問 <input> 并設(shè)置 value。將 app.component.ts 的內(nèi)容替換為以下內(nèi)容:

import {
  Component,
  ViewChild,
  AfterViewInit,
  ElementRef
} from '@angular/core';
@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements AfterViewInit {
  @ViewChild('someInput') someInput!: ElementRef;
  ngAfterViewInit() {
    this.someInput.nativeElement.value = 'Whale!';
  }
}

ngAfterViewInit 觸發(fā)時,<input> 的值將被設(shè)置為:

Whale!

父組件能夠設(shè)置子 DOM 元素的值。

使用 ViewChild 與子組件

ViewChild 使得訪問子組件并調(diào)用子組件可用的方法或訪問實例變量成為可能。

假設(shè)您有一個 PupComponent。

理想情況下,您將使用 @angular/cligenerate 您的組件:

ng generate component pup --flat --skip-tests

此命令將創(chuàng)建 pup.component.ts、pup.component.csspup.component.html 文件。并將該組件添加到 app.module.ts

import { PupComponent } from './pup.component';
...
@NgModule({
  declarations: [
    AppComponent,
    PupComponent
  ],
  ...
})

然后,在 PupComponent 中添加一個返回消息的 whoAmI 方法:

import { Component, OnInit } from '@angular/core';
@Component({
  selector: 'app-pup',
  templateUrl: './pup.component.html',
  styleUrs: ['./pup/component.css']
})
export class PupComponent implements OnInit {
  constructor() { }
  whoAmI() {
    return 'I am a pup component!';
  }
  ngOnInit(): void {
  }
}

接下來,在應用程序模板中引用子組件。將 app.component.html 的內(nèi)容替換為以下內(nèi)容:

<app-pup>pup works!</app-pup>

現(xiàn)在,您可以使用 ViewChild 在父組件類中調(diào)用 whoAmI 方法。將 app.component.ts 的內(nèi)容替換為以下內(nèi)容:

import {
  Component,
  ViewChild,
  AfterViewInit
} from '@angular/core';
import { PupComponent } from './pup.component';
@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css'],
})
export class AppComponent implements AfterViewInit {
  @ViewChild(PupComponent) pup!: PupComponent;
  ngAfterViewInit() {
    console.log(this.pup.whoAmI()); // I am a pup component!
  }
}

在瀏覽器中查看應用程序時,控制臺日志將顯示:

I am a pup component!

父組件能夠調(diào)用子組件的 whoAmI 方法。

結(jié)論

在本教程中,您使用了 ViewChild 來從父組件類中訪問指令、子組件和 DOM 元素。

如果引用動態(tài)更改為新元素,ViewChild 將自動更新其引用。

在需要訪問多個子元素的情況下,您應該使用 ViewChildren。

如果您想了解更多關(guān)于 Angular 的知識,請查看我們的 Angular 專題頁面,了解練習和編程項目。

到此這篇關(guān)于在 Angular 中使用 ViewChild 訪問子組件、指令或 DOM 元素的操作方法的文章就介紹到這了,更多相關(guān)Angular 使用 ViewChild 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Angular使用Restful的增刪改

    Angular使用Restful的增刪改

    今天小編就為大家分享一篇關(guān)于Angular使用Restful的增刪改,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2018-12-12
  • Angular請求防抖處理第一次請求失效問題

    Angular請求防抖處理第一次請求失效問題

    這篇文章主要介紹了angular請求防抖及處理第一次請求失效的相關(guān)資料,需要的朋友可以參考下
    2019-05-05
  • angular學習之動態(tài)創(chuàng)建表單的方法

    angular學習之動態(tài)創(chuàng)建表單的方法

    這篇文章主要介紹了angular學習之動態(tài)創(chuàng)建表單的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-12-12
  • 解決angular2 獲取到的數(shù)據(jù)無法實時更新的問題

    解決angular2 獲取到的數(shù)據(jù)無法實時更新的問題

    今天小編就為大家分享一篇解決angular2 獲取到的數(shù)據(jù)無法實時更新的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-08-08
  • 淺談Angular路由守衛(wèi)

    淺談Angular路由守衛(wèi)

    這篇文章主要介紹了淺談Angular路由守衛(wèi),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-08-08
  • AngularJS中過濾器的使用與自定義實例代碼

    AngularJS中過濾器的使用與自定義實例代碼

    這篇文章運用實例代碼給大家介紹了angularjs中過濾器的使用和自定義過濾器,對大家學習AngularJS具有一定的參考借鑒價值,感興趣的朋友們可以參考借鑒。
    2016-09-09
  • Angular Cookie 讀寫操作代碼

    Angular Cookie 讀寫操作代碼

    這篇文章主要介紹了Angular Cookie 讀寫操作代碼,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,感興趣的朋友跟隨小編一起看看吧
    2022-01-01
  • angular1配合gulp和bower的使用教程

    angular1配合gulp和bower的使用教程

    這篇文章主要介紹了angular1配合gulp和bower使用教程,本文給大家介紹的非常詳細,具有參考借鑒價值,需要的朋友可以參考下
    2018-01-01
  • Angular.js指令學習中一些重要屬性的用法教程

    Angular.js指令學習中一些重要屬性的用法教程

    這篇文章主要給大家介紹了關(guān)于Angular.js指令學習中一些重要屬性的用法教程,文中介紹的非常詳細,對大家具有一定的參考學習價值,需要的朋友們下面來一起看看吧。
    2017-05-05
  • AngularJs 利用百度地圖API 定位當前位置 獲取地址信息

    AngularJs 利用百度地圖API 定位當前位置 獲取地址信息

    本文主要介紹了AngularJs 利用百度地圖API 定位當前位置 獲取地址信息的方法步驟。具有一定的參考價值,下面跟著小編一起來看下吧
    2017-01-01

最新評論

繁昌县| 哈密市| 土默特左旗| 康马县| 万全县| 斗六市| 中宁县| 中江县| 沁阳市| 衢州市| 巩留县| 永福县| 清流县| 彭水| 兴义市| 成武县| 潜山县| 皮山县| 江山市| 晋中市| 扶沟县| 平利县| 通江县| 丰宁| 吴川市| 银川市| 扎兰屯市| 朝阳县| 嵩明县| 武乡县| 大丰市| 景洪市| 永安市| 宜州市| 林芝县| 麦盖提县| 河东区| 长海县| 上犹县| 胶南市| 四会市|