简体   繁体   English

如何在角度4中将值分配给子组件属性?

[英]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.

相关问题 如何在Angular 5中设置child(或nth child)组件的child的属性 - How to set property of child of child(or nth child) component in Angular 5 在 Angular 中使用子组件的属性? - Property using child component in Angular? 如何在 Angular 8 中将属性从父组件传递给子组件? - How to pass property from parent to child component in Angular 8? 角形| 当我使用2 ngIf时,如何读取子组件的属性? - angular | when I use 2 ngIf, how to read property of child component? 如何将角度异步数据绑定到子组件输入属性 - How to bind angular async data to child component input property 将值传递给子组件Angular 7 - Pass values to child component Angular 7 Angular4如何在确保父对象组件变量未定义之后从父组件变量中为其分配变量 - Angular4 how to assign variable in child component from parent component variable after reassuring that it is not undefined 如何在 Angular 中从父组件到子组件的 Map 值? - How to Map values from parent to child component in Angular? 如何在Angular2中的子组件上的模板中显示嵌套对象值? - How to display nested objects values in a template on a child component in Angular2? 如何在Angular 4中将值从子组件传递给父组件 - How to pass values from child to parent component in Angular 4
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM