- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我一直在研究这个问题并进行试验,但我无法就如何解决这个问题或者它是否是预期的行为提出一个结论性的答案。
前段时间,我按照 Angular 指南使用 componentFactoryResolver (https://v6.angular.io/guide/dynamic-component-loader) 创建动态组件。
我遇到的问题是动态创建的组件中的生命周期 Hook (ngAfterViewInit、ngOnInit 等)在鼠标移到相关组件上之前不会运行。这似乎是奇怪的行为。
这似乎与更改检测无关,因为手动运行更改检测 (this.cdRef.detectChanges()
) 不会触发 Hook 。
由于某种原因,Angular 文档中的示例不起作用,我无法创建最小复制,因此请在下面查看我的一些实现片段。一切正常,除了生命周期钩子(Hook)。
HTML 中的用法:
<div class="modal-body modal-dataFlow animated fadeIn" [hidden]="showNotification">
<ng-template appForms></ng-template>
</div>
import { Directive, ViewContainerRef} from '@angular/core';
@Directive({
selector: '[appForms]'
})
export class FormsDirective {
constructor(public viewContainerRef: ViewContainerRef) { }
}
import { Type } from '@angular/core';
export class FormItem {
constructor(public component: Type<any>, public data: any) { };
}
import { Injectable } from '@angular/core';
import { HttpcallerComponent } from '../views/dataflows/forms/httpcaller/httpcaller.component';
import { FormItem } from '../views/dataflows/form-item';
import { Constants } from '../views/dataflows/Constants';
import { Subject } from 'rxjs';
import { Observable } from 'rxjs/internal/Observable';
@Injectable()
export class FormsService {
myBool$: Observable<boolean>;
private boolSubject: Subject<boolean>;
constructor() {
this.boolSubject = new Subject<boolean>();
this.myBool$ = this.boolSubject.asObservable();
}
emitValue(param: boolean) {
this.boolSubject.next(param);
}
getForms() {
return [
new FormItem(HttpcallerComponent, { name: Constants.HTTP_CALLER_ENDPOINT_NAME, stepName: Constants.HTTP_CALLER_STEP_TYPE_NAME })
];
}
}
completeLoad(dataStepInfo, formItem) {
let componentFactory = this.componentFactoryResolver.resolveComponentFactory(formItem.component);
this.viewContainerRef.clear();
let componentRef = this.viewContainerRef.createComponent(componentFactory);
formItem.data.dataStepInfo = dataStepInfo;
formItem.data.flowId = this.flowID;
(<IForms>componentRef.instance).data = formItem.data;
(<IForms>componentRef.instance).parentForm = this.propertyForm;
this.childComponent = componentRef.instance;
this.componentCreated.next(this.maximiseRegex.test("app." + this.selectedEndpointType + "Step"));
if (dataStepInfo.endpointName === Constants.MERGE_STEP_ENDPOINT_NAME) {
this.dataFlowModal.show();
}
}
import { Component, OnInit, Input, OnDestroy, ChangeDetectorRef, ViewEncapsulation, AfterViewInit } from '@angular/core';
import { FormGroup, FormBuilder, Validators, FormGroupDirective } from '@angular/forms';
import { Observable, Subscription } from 'rxjs';
import { ModalService } from '../../../../services/modal.service';
import { HighlightTag } from 'angular-text-input-highlight';
import { Result } from '../../../../common/result';
@Component({
selector: 'app-httpcaller',
templateUrl: './httpcaller.component.html',
styleUrls: ['./httpcaller.component.scss'],
encapsulation: ViewEncapsulation.None
})
export class HttpcallerComponent implements OnInit, OnDestroy, AfterViewInit {
private confirmed: boolean = false;
private errorText: string;
private sub: Subscription;
private transmission;
public showConfirm: boolean = false;
public alertsDismiss: any = [];
public localHeaders;
private previousContentType;
private formSub: Subscription;
private receivedAuth: number;
private isViewInitialized: boolean = false;
private urlSubscription: Subscription;
@Input() parentForm: FormGroupDirective;
@Input() data: any;
tags: HighlightTag[] = [];
tagClicked: HighlightTag;
httpConfigForm: FormGroup;
private httpConfigSettings = {
httpConfig: null, httpHeaders: null, httpAuthorization: null
}
constructor(
private formBuilder: FormBuilder,
private modalService: ModalService,
private cdRef: ChangeDetectorRef) {
}
public configSelection: string;
public authList: any;
public disableButtons: boolean = false;
private confirmType: boolean = false;
private readonly onPremAgentKey: string = '{{OnPremAgent}}';
ngOnInit() {
if (this.isViewInitialized) {
return;
}
this.isViewInitialized = true;
this.sub = this.modalService.myBool$.subscribe((newBool: boolean) => {
this.confirmed = newBool;
if (this.confirmed && !this.confirmType) {
this.runDeleteAuth();
}
else {
this.checkContentType(this.confirmed);
this.confirmed = false;
}
this.confirmType = false;
});
//this.transmissionId
this.getContentTypes();
this.GetCommonFormatTypes();
this.cdRef.detectChanges();
this.addTags();
}
ngAfterViewInit() {
console.log("AfterViewInit running");
}
最佳答案
这样做可以点燃工厂 View :
constructor(private appRef: ApplicationRef) {}
在componentRef下面:
this.appRef.attachView(componentRef.hostView);
关于javascript - Angular 6 生命周期钩子(Hook)不在动态创建的组件上运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60920226/
我正在开发一个使用多个 turtle 的滚动游戏。玩家 turtle 根据按键命令在 Y 轴上移动。当危害和好处在 X 轴上移动时,然后循环并改变 Y 轴位置。我尝试定义一个名为 colliding(
我不明白为什么他们不接受这个作为解决方案,他们说这是一个错误的答案:- #include int main(void) { int val=0; printf("Input:- \n
我正在使用基于表单的身份验证。 我有一个注销链接,如下所示: 以及对应的注销方法: public String logout() { FacesContext.getCurren
在 IIS7 应用程序池中有一个设置 Idle-time out 默认是 20 分钟,其中说: Amount of time(in minutes) a worker process will rem
我是一名优秀的程序员,十分优秀!