简体   繁体   English

使用Angular中的ViewChild()获取元素

[英]Getting an element using ViewChild() in angular

I want to get an table element which is inside an modal I am providing the modal html code bellow and how I am trying to access the data table. 我想获取模态内部的表格元素,我在下面提供模态html代码以及如何尝试访问数据表。 The datatable is of primeng 数据表是主要的


  <ng-template #industryModal>
<div class="modal-body">
  <h4>{{'Instance.IndustryModal.Header' | translate }}</h4>
  <p class="m-t-20">{{'Instance.IndustryModal.Branch' | translate }}: {{instanceObj['personaldata.Branch']}}</p>
  <p-table #dt [value]="industries" [paginator]=true [rows]="10" paginatorPosition="bottom" 
    [sortField]="sortField" [sortOrder]="sortOrder" styleClass="industry-data" [first]="first">

    <ng-template pTemplate="caption">
      <i class="fa fa-search" style="margin:4px 4px 0 0"></i>
      <input type="text" pInputText size="50" placeholder="{{'Common.Search' | translate}}"
        (keyup)="dt.filterGlobal($event.target.value)" style="width:auto">
    </ng-template>
    <ng-template pTemplate="body" let-industry>
      <tr>
          <td>
            <div class="industry-list-item" (click)="selectIndustry(industry)"
            [ngClass]="{'select': (selectedIndustry && industry.Name == selectedIndustry.Name)}">
            <div class="vin-number">{{industry.Id}}</div>
            <div class="industry-brand">{{industry.Name}}</div>
            <div class="industry-warning warning-{{industry.Warning}}">
              <i class="fa fa-exclamation-triangle warning-message" *ngIf="industry.Message != null"
                pTooltip="{{industry.Message}}" tooltipPosition="top" tooltipZIndex="1100"></i>
              <span class="">{{industry.Warning}}</span>
              <i class="fa fa-check selected-icon"
                *ngIf="(selectedIndustry && industry.Name == selectedIndustry.Name)"></i>
            </div>
          </div>
          </td>
      </tr>
  </ng-template>
  </p-table>
  <div class="clearfix">&nbsp;</div>
  <div class="float-right">
    <button type="button" class="btn secondary-btn m-r-16"
      (click)="closeIndustryModal()">{{'Instance.Cancel' | translate}}</button>
    <button type="button" class="btn btn-primary primary-btn"
      (click)="saveIndustry()">{{'Instance.Save' | translate}}</button>
  </div>
</div>
</ng-template>

And I am trying to access the data table using the following way. 我正在尝试使用以下方式访问数据表。

   @ViewChild('dt') dataveiw: BsModalRef;

But when I console dataveiw it shows undefined. 但是当我控制台dataveiw时,它显示未定义。

When using ViewChild you need to add template reference variable something like this using #dt 当使用ViewChild您需要添加模板引用变量像这样使用#dt

<ng-template #dt pTemplate="caption"></ng-template>

Because you are define reference name dt in your code 因为您在代码中定义了引用名称dt

@ViewChild('dt') dataveiw: BsModalRef;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM