1

How can I update a specific object which is passed to child from parent. In my Parent Template I for example have 2 Child Templates which I define like this.

<app-ig-dropdown
       [compInfo]="{guid :'820E04E0-8084-4D9C-A268-D8C0D21E74F6',
                    width:'350px',
                    placeHolder: ' -- Select --',
                    fieldText:'Social Media 1'}"
        formControlName="combo1"
        >
</app-ig-dropdown>
<app-ig-dropdown
        [compInfo]="{guid :'820E04E0-8084-4D9C-A268-D8C0D21E74F6',
                    width:'350px',
                    placeHolder: ' -- Select --',
                    fieldText:'Social Media 2'}"
        formControlName="combo2"
>
</app-ig-dropdown> 

The question is, how can I now update the value from my component file. I want to be able to access the compInfo object read and modify it and how do I access the different objects based on the formControleName?

4 Answers 4

1

Please take a look.

on your tmpl:

<div *ngFor="let compInfo of combInfos; let i = index;" [attr.data-index]="i">
    <app-ig-dropdown
       [compInfo]="compInfo"
       (update)=updateCompInfo(i)
    >
    </app-ig-dropdown>
</div>

on app-ig-dropdown.ts:

@Output() update: EventEmitter<void>;

//in some function

this.update.emit();

on your ts:

//compInfo class

class CompInfo {
  public guid: string;
  public width: string';
  public placeHolder: string;
  public fieldText: string;
}

////////////////////////

public compInfos: Array<CompInfo> = [
 {
  guid :'820E04E0-8084-4D9C-A268-D8C0D21E74F6',
  width:'350px',
  placeHolder: ' -- Select --',
  fieldText:'Social Media 1'
 },
 {
  guid :'820E04E0-8084-4D9C-A268-D8C0D34E74F6',
  width:'350px',
  placeHolder: ' -- Select --',
  fieldText:'Social Media 2'
 }
]

public getCompInfo(guid): CompInfo {
  return this.compInfos.find(c => c.guid === guid);
}

public setCompInfo(compInfo): void {
 this.compInfos = this.compInfos.filter(c => c.guid!== compInfo.guid);
 this.compInfos.push(compInfo);
} 

//example function
public updateCompInfo(index): void {
 let compInfo: CompInfo = this.compInfos[index]; 
 compInfo.width = '100px';
 this.setCompInfo(compInfo);
} 
Sign up to request clarification or add additional context in comments.

5 Comments

how would i then update a value based o the control name. Also based on your example it seems there is no way to specify the object values in the template
this still requires me to know the index in array of the control before i can update it. If i know the index i could call a simple this.compInfos[0].fieldText ="demo";
Did you try to use *ngFor?
And you need to output update function in child component like above
@MisterniceGuy how is it going?
0

You can't update the parent component. You can however send an event to the parent. In your child component, add

@Output() update = new EventEmitter<CompInfo>();

To update the object, emit an event:

this.update.emit({ ...new_values })

You should define the objects in the parent component:

public firstCompInfo: CompInfo = {
  guid: '820E04E0-8084-4D9C-A268-D8C0D21E74F6',
  width: '350px',
  placeHolder: ' -- Select --',
  fieldText: 'Social Media 1',
};

Then listen to the update event in the template:

<app-ig-dropdown
  formControlName="combo1"
  [compInfo]="firstCompInfo"
  (update)="firstCompInfo = $event">
</app-ig-dropdown>

CompInfo is defined like this:

interface CompInfo {
  public guid: string;
  public width: string;
  public placeHolder: string;
  public fieldText: string;
}

Comments

0

Ok after some more research and playing around I figured out a much simpler way..

Declare @ViewChild in Parent

  @ViewChild('combo1', { static: true }) combo1: IgDropDownComponent;
  @ViewChild('combo2', { static: true }) combo2: IgDropDownComponent;

Then I can get info of a child via

  console.log(this.combo2.compInfo)

And if I want to set some elements in the object

  this.combo1.compInfo.fieldText = 'Set Media 1';
  this.combo2.compInfo.fieldText = 'Set Media 2';

No need for Index or trying to find a value for a control. Just access the element directly get the value or update it from parent.

Comments

0

I ended up using the below code to update any values as it doesn't require to know index beforehand:

public updateArrayElement(formControlValue, element, newValue) {
    this.index = this.compInfos.findIndex(x => x.formControlName === formControlValue);
    this.compInfos[this.index][element] = newValue;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.