zoukankan      html  css  js  c++  java
  • [Angular] Difference between ViewChild and ContentChild

    *The children element which are located inside of its template of a component are called *view children *. On the other hand, **elements which are used between the opening and closing tags of the host element of a given component are called *content children **.

    ViewChild:

    Auth-form.component's template:

        <div>
          <form (ngSubmit)="onSubmit(form.value)" #form="ngForm">
            <ng-content select="auth-remember"></ng-content>
            <auth-message></auth-message>
          </form>
        </div>

    Here we can use ViewChild to access <auth-message></auth-message> component, because it is a child component of auth-form component.

    ContentChild:

    auth-form component:

          <auth-form 
            (submitted)="loginUser($event)">
            <auth-remember
              [role]="'checkbox1'"  
              (checked)="rememberUser($event)">
            </auth-remember>
          </auth-form>

    Notice here <auth-remember> is passed into <auth-form> by content projection. 

          <form (ngSubmit)="onSubmit(form.value)" #form="ngForm">
            <ng-content select="auth-remember"></ng-content>
            <auth-message></auth-message>
          </form>
        </div>

    So here if we want to access <auth-remeber> inside auth-form component, we have to use @ContentChild.

  • 相关阅读:
    EEPROM芯片AT2402驱动
    FPGA 状态机(FSM)的三段式推荐写法
    1602液晶驱动
    Bresenham快速画直线算法
    I2C总线驱动程序
    从数据库中取时间类型显示
    C# 页面关联类似模式窗口
    C# 页面javascript 页面跳转刷新
    网页有趣的时间显示控件
    DataSet
  • 原文地址:https://www.cnblogs.com/Answer1215/p/6417834.html
Copyright © 2011-2022 走看看