jeudi 27 septembre 2018

check checkboxes Angular 5

I'm new to angular so please excuse me if I'm going to say / ask something very stupid. I followed this example to add checkboxes to my form: enter link description here

Everything works good, I can save the ids of my checkboxes into the database. The problem is that when I try to modify the form I can't check the checkboxes that were checked before and saved into my database ( a list of strings, I know it's not ok but for now I'll keep it like this). I receive the list, I transform it into an array to iterate and then I tried to set value to true to the controls but nothing is working. Can someone help me? These is my file: addmodify.component.ts :`

public natura = [
        { id: 1, name: 'si' },
       { id: 2, name: 'af' },
       { id: 3, name: 'erg'},
       { id: 4, name: 'rid'},
       { id: 5, name: 'ridc' },
       { id: 6, name: 'qual'},
       { id: 7, name: 'Ridte'},
       { id: 8, name: 'Alt'}
    ];
 constructor(
        private _fb: FormBuilder,
        private _avRoute: ActivatedRoute,
        private _suggerimentoService: SuggerimentoService,
        private _nuovoPartecipanteService: NuovoPartecipanteService,
        private toasterService: ToasterService,
        private translate: TranslateService,
        private _router: Router) {
            if (this._avRoute.snapshot.params["id"]) {
                this.id = this._avRoute.snapshot.params["id"];
            }
            else
                this.title = "Inserisci";

          const controls = this.natura.map(c => new FormControl(false));

            this.suggerimentoForm = this._fb.group({
                id: 0,
                partecipanti: ['', [Validators.required]],
                stabilimento: [''],
                approvatoreID: 0,
                titolo: ['', [Validators.required]],
                descrizione: ['', [Validators.required]],
                soluzione: ['', [Validators.required]],
                natura: new FormArray(controls, minSelectedCheckboxes(1)),
                vorrei: [''],
                partecipantiLista: ['']
        });

    }
  ngOnInit() {
      if (this.id > 0) {

          this._suggerimentoService.getSuggerimentoById(this.id)
              .subscribe(resp => {    
                  const controls = this.natura.map(c => new FormControl(false));
                  const listNatura = resp.natura.split(',');
                  listNatura.forEach(function (value) {
                      console.log('valore=',value);
                      controls[0].setValue(true);
                  }); 


                  resp.natura = this.natura;





                  this.suggerimentoForm.setValue(resp);


              }
                  , error => this.errorMessage = error);
      }  
    }
`

and this is my html file:

<form class="k-form" [formGroup]="suggerimentoForm" (ngSubmit)="save()" #formDir="ngForm" novalidate>




Aucun commentaire:

Enregistrer un commentaire