-1

I need to create this:

instructions: ['value one' , 'value two' , 'value three'];

I am using formArray and check my code on stackblitz first: https://stackblitz.com/edit/formarray-obj?file=app/app.component.ts

html:

<div class="form">
    <form id="form" [formGroup]="filmForm" (ngSubmit)="save()">
        <button type="button" (click)="addItem()"> add new row</button>
        <div formArrayName="instructions" *ngFor="let a of filmForm.get('instructions').controls; let i = index">
            <div [formGroupName]="i" style="margin-bottom: 10px;">
                <label for="name">Name:</label>
                <input type="text" name="name" formControlName="instructions">
                <br><br>
            </div>
        </div>
        <div class="submit">
            <button type="submit">Save</button>
        </div>
    </form>
</div>

TS:

registerForm: FormGroup;
submitted = false;
instructions: FormArray;
filmForm: FormGroup;
constructor(private formBuilder: FormBuilder) { }

ngOnInit() {
    this.filmForm = this.formBuilder.group({
        instructions: this.formBuilder.array([this.createItem()])
    })
}

createItem() {
    return this.formBuilder.group({
        instructions: ['']
    })
}

addItem() {
    this.instructions = this.filmForm.get('instructions') as FormArray;
    this.instructions.push(this.createItem());
}

save() {
    console.log(this.filmForm.value)
}

I am sending array of object: check stackblitz console.log I am send like

instructors = [{instructs : 'value one'} , {instructs : 'value one'}..]

But I need to sent:

 instructors = ["value one" , "value two"];
5
  • I don't think so till Angular v8. #Following Commented Jun 16, 2020 at 11:01
  • I don't understand ? What about v8 ? Commented Jun 16, 2020 at 11:04
  • I mean Till Angular version 8. Commented Jun 16, 2020 at 11:12
  • This answer might help you Commented Jun 16, 2020 at 11:16
  • You can use rxjs to format your data: console.log({ instructions: this.filmForm.value.instructions.map(e=> e.instructions) });. have a look on my stackblitz: stackblitz.com/edit/formarray-obj-kkszod Commented Jun 16, 2020 at 11:37

2 Answers 2

0

There are two ways in which you go ahead with this.

1 - Using .map in save()

For the desired output, you can use the .map in save() function:

save() {
  const instructions = this.filmForm.value.instructions.map(inst => inst. instruction);

  console.log(instructions);
}

Desired Output will be:

["value one" , "value two"]

2 - Using a tags input control like ng-select

Your use case looks like a tags control use case for instructions. There is a popular library @ng-select/ng-select which has a tags control.

You can see the usage of this library for your use case on this stackblitz.

Approach 1 is the easiest one to do but I would suggest you use tags control in approach 2 for good user experience.

Sign up to request clarification or add additional context in comments.

2 Comments

Ok I got this value...but how to append to my form...this values....
Could you explain what are you trying to do with the values? And also, which approach are you following?
0

You can try this.. i had the same issue.

Make the Form

form = new FormGroup({
instructorsArray: new FormArray([]),
})

  get instructors(): FormGroup {
    return FormControl('')
  }

 addInstructors() {
    this.instructors.push(this.instructors);
  }

3 Comments

What is this.pegadio??
sorry, this.instructors.push(this.instructors);
made a change to better understand.

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.