[英]How to assign the values to the child component property in angular 4?
While using ngif, I not able to get the element reference of the child component. 使用ngif时,我无法获取子组件的元素引用。 after a lot of searches, to get the element, need to use view children instead of view child.The thing is I am able to get the child reference but not able to assign the values to child properties.
经过大量搜索后,要获取元素,需要使用view children而不是view child。问题是我能够获取child引用,但无法将值分配给child属性。
here CorrespondenceAddressComponent is my child component. 这里CorrespondenceAddressComponent是我的子组件。 I am assigning the values to one of the correspondence address component object(AddressComponentMetadataDto ) in manage person component-SetControlsReadModeInPerson method.
我将值分配给管理人员组件SetControlsReadModeInPerson方法中的对应地址组件对象之一(AddressComponentMetadataDto)。 I am getting undefined while assigning the values or calling the child component methods.
在分配值或调用子组件方法时,我变得不确定。
ManagePersonComponent: ManagePersonComponent:
export class ManagePersonComponent extends ManagePartyComponent implements AfterViewInit {
ngAfterViewInit(): void {
this.CurrentPartyType = CurrentPartyTypeEnum.Person;
this.GetInitialDataFromService();
this.SetControlsReadModeInPerson(this.IsReadonly);
}
SetControlsReadModeInPerson(isReadOnly: boolean): void {
this.CorrespondenceAddressComponent.AddressComponentMetadataDto.IsPostalCodeDisabled = isReadOnly;
}
ManagePartyComponent(base component of ManagePersonComponent): ManagePartyComponent(ManagePersonComponent的基本组件):
export class ManagePartyComponent extends ProjectPageComponentBase {
@ViewChildren(CorrespondenceAddressComponent) CorrespondenceAddressComponent: CorrespondenceAddressComponent;
}
Html: HTML:
<form #form="ngForm" [ngClass]="{'form-submitted' : form.submitted }" (ngSubmit)="SaveAndNavigate()">
<toolbar [ShowSaveButton]="!IsReadonly"></toolbar>
<p-tabView [(activeIndex)]="SelectedTabIndex">
<p-tabPanel [header]="'Caption.CRM.CorrespondenceAddress' | translate">
<correspondence-address [ParentForm]="form"></correspondence-address>
</p-tabPanel>
</p-tabView>
</form>
CorrespondenceAddressComponent: CorrespondenceAddressComponent:
export class CorrespondenceAddressComponent extends ProjectPageComponentBase {
AddressComponentMetadataDto= new AddressComponentMetadataDto();
ngOnInit():void {
}
}
ViewChildren will return a QueryList, whereas ViewChild returns a single match. ViewChildren将返回QueryList,而ViewChild返回单个匹配项。
If you really need to use ViewChildren, make sure you type it correctly: 如果您确实需要使用ViewChildren,请确保正确键入它:
export class ManagePartyComponent extends ProjectPageComponentBase {
@ViewChildren(CorrespondenceAddressComponent)
CorrespondenceAddressComponents: QueryList<CorrespondenceAddressComponent>;
}
And treat it like an enumerable: 并将其视为枚举:
SetControlsReadModeInPerson(isReadOnly: boolean): void {
const correspondenceAddressComponent = this.CorrespondenceAddressComponents.first;
correspondenceAddressComponent.AddressComponentMetadataDto.IsPostalCodeDisabled = isReadOnly;
}
Otherwise, use ViewChild instead: 否则,请改用ViewChild:
export class ManagePartyComponent extends ProjectPageComponentBase {
@ViewChild(CorrespondenceAddressComponent)
CorrespondenceAddressComponents: CorrespondenceAddressComponent;
}
So that you don't need to work with enumerable objects. 这样您就不需要使用可枚举的对象。 If you try and reference
AddressComponentMetadataDto
on a QueryList, you'll get undefined, and further referencing IsPostalCodeDisabled
on undefined will give you your error. 如果尝试在
AddressComponentMetadataDto
上引用AddressComponentMetadataDto
,则将得到未定义,而在未定义上进一步引用IsPostalCodeDisabled
将给您带来错误。
I'm going to go out on a limb and say this is your problem. 我要弯腰说这是你的问题。
This stackblitz should be rather close to what you're trying to do: https://stackblitz.com/edit/angular-jnqwma?file=src%2Fapp%2Fcomponents%2Fparent-component.ts 该stackblitz应该与您要执行的操作非常接近: https ://stackblitz.com/edit/angular-jnqwma?file=src%2Fapp%2Fcomponents%2Fparent-component.ts
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.