简体   繁体   English

Angular4:ng-template中的组件引用

[英]Angular4: Component reference inside ng-template

I am new to angular 4. I want to get handle of a component(validation-alerts) inside ng-template 我是angular 4的新手。我想获取ng-template中的组件(validation-alerts)句柄

<ng-template #content let-c="close" let-d="dismiss" size="600px" >  
    <validation-alerts  [formGroup]="requestTypeForm"></validation-alerts>  
    <div class="ui-dialog-titlebar ui-widget-header ui-corner-all ui-helper-clearfix head" tabindex="-1">
        <span id="ui-id-6" class="ui-dialog-title popup-title">Client Name Look Up </span>
        <button (click)="d('Cross click')" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-icon-only ui-dialog-titlebar-close" 
        role="button" aria-disabled="false" title="Close">
        <span class="ui-button-icon-primary ui-icon ui-icon-closethick popup-close-icon"></span><span class="ui-button-text">Close</span>
        </button>
    </div>
    <form (ngSubmit)="onSubmit()" [formGroup]="requestTypeForm">
    <div id="scrollContainer-popup" class="popup ui-dialog-content ui-widget-content" style="width: auto; min-height: 0px; max-height: none; height: auto; display: block; padding: 0px 10px 0px">
        <div class="body">
            <p class="mb"><b>Select the matching client name:</b></p>
            <input type="text" formControlName="clientId" name="clientId" 
                    id="clientId" style="width:30%" />

            <div class="btnBar nbdr" style="padding-top: 0px;" >
                <a (click)="c('close')" class="btn3"><span>Cancel</span></a>      
                <button type="button" class="btn" (click)="c('submit')">Select</button>  
            </div>
        </div>
    </div>
    </form> 
</ng-template>

I tried using @ViewChild, @ViewChildren but getting undefined in AfterContentInit method. 我尝试使用@ ViewChild,@ ViewChildren,但是在AfterContentInit方法中未定义。

 @ViewChild(ValidationAlertsComponent)
  private validationAlerts: ValidationAlertsComponent;

Component Class 组件类别

@Component({
  selector: 'provision-modal',
  templateUrl: 'provision.modal.html',
  encapsulation: ViewEncapsulation.None,
  styles : [ '.namelookup .modal-content {  width: 600px;}']
 })
export class ProvisionModal implements AfterContentInit {
  @ViewChild('content')  contentTemplate: TemplateRef<any>;

   @ViewChild(ValidationAlertsComponent)
  private validationAlerts: ValidationAlertsComponent;

     ngAfterContentInit() {
     console.log('Ng after content init '+ this.validationAlerts);
  }

You should access the view child after the ngAfterViewInit life cycle hook. 您应该在ngAfterViewInit生命周期挂钩之后访问视图子级。

See https://angular.io/guide/lifecycle-hooks 参见https://angular.io/guide/lifecycle-hooks

So change 所以改变

ngAfterContentInit() {
     console.log('Ng after content init '+ this.validationAlerts);
}

to

ngAfterViewInit() {

}

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

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