zoukankan      html  css  js  c++  java
  • Angular 2 ViewChild & ViewChildren

    、ViewChild

    ViewChild 是属性装饰器,用来从模板视图中获取匹配的元素。视图查询在 ngAfterViewInit 钩子函数调用前完成,因此在 ngAfterViewInit 钩子函数中,才能正确获取查询的元素。

    1.@ViewChild 使用模板变量名

    import { Component, ElementRef, ViewChild, AfterViewInit } from '@angular/core';
    
    @Component({
      selector: 'my-app',
      template: `
        <h1>Welcome to Angular World</h1>
        <p #greet>Hello {{ name }}</p>
      `,
    })
    export class AppComponent {
      name: string = 'Semlinker';
    
      @ViewChild('greet')
      greetDiv: ElementRef;
    
      ngAfterViewInit() {
        console.dir(this.greetDiv);
      }
    }

    2.@ViewChild 使用模板变量名及设置查询条件

    import { Component, TemplateRef, ViewChild, ViewContainerRef, AfterViewInit } from '@angular/core';
    
    @Component({
      selector: 'my-app',
      template: `
        <h1>Welcome to Angular World</h1>
        <template #tpl>
          <span>I am span in template</span>
        </template>
      `,
    })
    export class AppComponent {
    
      @ViewChild('tpl')
      tplRef: TemplateRef<any>;
    
      @ViewChild('tpl', { read: ViewContainerRef })
      tplVcRef: ViewContainerRef;
    
      ngAfterViewInit() {
        console.dir(this.tplVcRef);
        this.tplVcRef.createEmbeddedView(this.tplRef);
      }
    }

    3.@ViewChild 使用类型查询

    //child.component.ts
    import { Component, OnInit } from '@angular/core';
    
    @Component({
        selector: 'exe-child',
        template: `
          <p>Child Component</p>  
        `
    })
    export class ChildComponent {
        name: string = 'child-component';
    }
    //app.component.ts
    import { Component, ViewChild, AfterViewInit } from '@angular/core';
    import { ChildComponent } from './child.component';
    
    @Component({
      selector: 'my-app',
      template: `
        <h4>Welcome to Angular World</h4>
        <exe-child></exe-child>
      `,
    })
    export class AppComponent {
    
      @ViewChild(ChildComponent)
      childCmp: ChildComponent;
    
      ngAfterViewInit() {
        console.dir(this.childCmp);
      }
    }

    以上代码运行后,控制台的输出结果:

    二、ViewChildren

    ViewChildren 用来从模板视图中获取匹配的多个元素,返回的结果是一个 QueryList 集合。

    @ViewChildren 使用类型查询

    import { Component, ViewChildren, QueryList, AfterViewInit } from '@angular/core';
    import { ChildComponent } from './child.component';
    
    @Component({
      selector: 'my-app',
      template: `
        <h4>Welcome to Angular World</h4>
        <exe-child></exe-child>
        <exe-child></exe-child>
      `,
    })
    export class AppComponent {
    
      @ViewChildren(ChildComponent)
      childCmps: QueryList<ChildComponent>;
    
      ngAfterViewInit() {
        console.dir(this.childCmps);
      }
    }

    以上代码运行后,控制台的输出结果:

    文章转自:https://segmentfault.com/a/1190000008695459

  • 相关阅读:
    linux学习17 运维核心技能-Linux系统下用户权限管理
    linux学习16 Linux用户和组管理命令演练和实战应用
    linux学习15 Linux系统用户和组全面讲解
    linux学习14 Linux运维高级系统应用-glob通配及IO重定向
    linux学习13 Linux运维常用文件管理命令及系统变量基础
    linux学习12 bash的常见特性及文本查看命令实战
    linux学习11 Linux基础命令及命令历史
    linux学习10 Linux目录结构和根文件系统全面讲解
    【Hadoop离线基础总结】Hive调优手段
    【Hadoop离线基础总结】Hive的基本操作
  • 原文地址:https://www.cnblogs.com/sghy/p/7092955.html
Copyright © 2011-2022 走看看