0

I'm working with Angular 8 and FormGroup, I got a little issue I can't fix, in my webpage I'm trying to render values inside a <select> based on another <select>.

Here's the JSON I recieve : JSON response

I'm trying to render "currencies" based on "ListPricelist"

Here's my HTML :

  <div  class="flex1 pl10">
   <select formControlName="pricelist" class="form-control" [ngClass]="{'is-invalid': submitted && 
      f.pricelist.errors}">
    <option value selected="selected">Select a pricelist</option>
    <option   name="pricelist_id" ngClass="pricelist_id"  *ngFor="let p of filter.ListPricelist; let i = index" id="pricelist_id" >{{i}}{{p.label}}</option>
  </select>

  <div *ngIf="submitted && f.pricelist.errors" class="invalid-feedback">
    <div *ngIf="f.pricelist.errors.required">Pricelist is required</div>
  </div>

  <select  formControlName="currency" class="form-control">
    <option value selected="selected">Select a currency</option>
    <option [value]="currency_id" *ngFor="let c of registerForm.value.pricelist.currencies" id="currency_id">{{c.code}}</option>
  </select>
</div>      

But in my page it doesn't render and I can only select List Pricelist

Don't hesitate to tell me if you need the TypeScript file and thank's for the help in advance

EDIT : Here's my TS file

import {Component, Injectable, OnInit} from '@angular/core';
import {Form, FormBuilder, ReactiveFormsModule, FormGroup, Validators} from 
"@angular/forms";
import {HttpClient} from '@angular/common/http';
import {ActivatedRoute, Router} from '@angular/router';




 @Component({
   selector: 'app-project',
   templateUrl: './project.component.html',
   styleUrls: ['./project.component.scss'],
 })

 @Injectable({
   providedIn: 'root'
  })


 export class ProjectComponent implements OnInit {


   ngOnInit() {
    this.requiredFields()
    }

filter = JSON.parse(window.localStorage.getItem('filters'));
filterCurrency = this.filter;
currencies: Array<any>;
 params: {
  ListPricelist: Array<{
    currencies: Array<{
      id: number;
      code: string;
      }>
    }>;
  };

 get f() {
  return this.registerForm.controls;}

 requiredFields(){
   this.registerForm = this.formBuilder.group(
  {
    name: ['',[Validators.required]],
    country: ['',[Validators.required]],
    pricelist_id: [''],
    pricelist: ['',[Validators.required]],
    currency: ['',[Validators.required]],
    status: ['',[Validators.required]],
    tarif: ['',[Validators.required]],
    hauteur: ['',[Validators.required]],
    supp: ['',[Validators.required]],
    mainO: ['',[Validators.required]]
    }
  )
 }
  constructor(private httpClient: HttpClient,
           private activatedRoute: ActivatedRoute,
           private formBuilder: FormBuilder,
           private router: Router,
           ){}

  }

export interface getResponse {
  ListPricelist: Array<{
   currencies: Array<{
   id: number;
   code: string;
   }>
  }>;

 }
3
  • 1
    pls you show us you .ts file Commented Nov 26, 2019 at 13:49
  • 1
    can you please add you .ts file code. Commented Nov 26, 2019 at 13:49
  • 1
    I edited the post with it Commented Nov 26, 2019 at 14:08

1 Answer 1

1

I reduced your example a little bit:

<div>
  <select (change)="selectedPriceListIndex = $event.target.value">
    <option value="-1" selected="selected">
      Select a pricelist
    </option>
    <option *ngFor="let p of filter.listPriceList; let i = index" [value]="i">
      {{ p.label }}
    </option>
  </select>

  <select>
    <option value selected="selected">
      Select a currency
    </option>
    <option *ngFor="let c of filter.listPriceList[selectedPriceListIndex]?.currencies">
      {{ c.code }}
    </option>
  </select>
</div> 

See stackblitz: https://stackblitz.com/edit/angular-kj3l5a

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

3 Comments

Thank you alot, it work as expected, can you explain me a bit what I was doing wrong please ?
It's hard to say without a stackblitz. A wild guess: Your first option iteration should have [value]="p"
Oh alright I think I was trying to use only the index value from my ngFor but I wasn't storing it in [value] so the second select was rendering nothing, thank you!

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.