0

I am trying to implement dynamic forms based on the example demonstrated in How to make generic component with form control input for Angular Forms in app. However, how do I load an asynchronous list for a select control? And how do I fill in a second select based on the first?

  async loadGender() {
    return of<any>([
        { value: "", label: "Bitte auswählen", selected: true },
        { value: "0", label: "männlich" },
        { value: "1", label: "weiblich" }
      ]).pipe(
      delay(2000)
    );
  }
    async loadStyle() {
    return of<any>([
        { value: "",  gender: '' , label: "X", selected: true },
        { value: "0", gender: '0',  label: "Y" },
        { value: "1", gender: '1',  label: "Z" }
      ]).pipe(
      delay(2000)
    );
  }

2 Answers 2

0
let first= this.loadGender().map(res => res.json());
let second= this.loadStyle().map(res => res.json());

Observable.forkJoin([first, second]).subscribe(results => {
  console.log('my new result',results)
});

This how to combine two observables

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

3 Comments

While this code may solve the question, including an explanation of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please edit your answer to add explanations and give an indication of what limitations and assumptions apply. From Review
I apologize, when writing the question I ended up copying the wrong link. I am posting here the correct link to the generic reactive form. stackblitz.com/edit/…
I can't edit the answer to change the wrong link. The correct link is this: stackblitz.com/edit/…
0

I presume, because of the link you referenced, you're using reactive forms.

You want to look at the valueChanges observable, which emits a value each time our form control value changes.

By using the emitted value, we can fetch an updated list of options for our second select element.

app.component.ts:

export class AppComponent {
  public carsForm: FormGroup;
  public cars$: Observable<any[]>;
  public models$: Observable<any[]>;

  private carFormControl = new FormControl("");
  private modelFormControl = new FormControl("");
  private carChangeSubscription: Subscription;

  constructor(private fb: FormBuilder) {}

  ngOnInit() {
    this.carsForm = this.fb.group({
      car: this.carFormControl,
      model: this.modelFormControl
    });

    this.cars$ = this.getCars();

    this.carChangeSubscription = this.carFormControl.valueChanges.subscribe(
      carId => {
        this.modelFormControl.setValue("");
        this.models$ = this.getModels(+carId);
      }
    );
  }

  ngOnDestroy() {
    this.carChangeSubscription.unsubscribe();
  }

  private getCars() {
    return of([
      {
        id: 1,
        name: "MERCEDES-BENZ"
      },
      {
        id: 2,
        name: "VOLVO"
      },
      {
        id: 3,
        name: "AUDI"
      },
      {
        id: 4,
        name: "HONDA"
      }
    ]);
  }

  private getModels(carId: number) {
    return of([
      {
        id: 1,
        carId: 1,
        name: "SL55 AMG"
      },
      {
        id: 2,
        carId: 2,
        name: "C70"
      },
      {
        id: 3,
        carId: 3,
        name: "S4"
      },
      {
        id: 4,
        carId: 4,
        name: "CR-V"
      }
    ]).pipe(
      map(models => {
        return models.filter(x => x.carId === carId);
      })
    );
  }
}

app.component.html

<form [formGroup]="carsForm">
   <select formControlName="car">
    <option value="">Please select</option>
    <option *ngFor="let car of cars$ | async" [value]="car.id">
      {{car.name}}
    </option>
  </select>
  <br /><br />
  <select formControlName="model">
    <option value="">Please select</option>
    <option *ngFor="let model of models$ | async" [value]="model.id">
      {{model.name}}
    </option>
  </select>
</form>

The AsyncPipe subscribes to our two data observables.

We unsubscribe from the valueChanges observable when the component is destroyed.

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.