0

Estoy creando varios textarea con una pregunta asociada. Ahora necesito guardar esta información para poder guardarla en una BBDD.

El código que tengo es el siguiente:

<ion-list text-wrap *ngFor="let Quest of navParams.data.question:let i=index">
  <ion-item>
    <ion-label>{{Quest.enunciado}}</ion-label>
    <ion-textarea placeholder="Escribe la respuesta"> </ion-textarea>
  </ion-item>
</ion-list>

1 respuesta 1

1

Puedes hacer una función para insertar las preguntas/respuestas en un arreglo a medida que vas modificando, acá te hice un ejemplo:

import { Component } from '@angular/core';

@Component({
  selector: 'my-app',
  template: `
  <div *ngFor="let question of questions">
     <h3>{{question}}</h3>
     <textarea (blur)="saveResponse(question, $event.target.value)"> </textarea>
  </div>

  <br>
  <h2>Respuestas</h2>
  <div *ngFor="let res of responses">
     <h3>{{res.question}}</h3>
     <h3>{{res.response}}</h3>
  </div>
`
})
export class AppComponent  {
  questions = [ "pregunta 1?", "pregunta 2?", "pregunta 3?", "pregunta 4?"];
  responses = [];

  saveResponse(quest, res) {
    this.responses.push({ question: quest, response: res});
  }
}

https://stackblitz.com/edit/angular-uvu5qp

3
  • Muchas gracias por tu aportación. En cuanto pueda intentaré adaptar el código en mi proyecto. El problema que veo que me va haciendo "push" y me crea un vector infinito de respuestas. Intentaré aprovechar el valor "i" que tengo y solucionarlo.
    – David_helo
    el 8 may. 2019 a las 10:36
  • Correcto, también podrías verificar si "question" ya existe dentro del arreglo, y en caso de ser así le modificas la respuesta en vez de hacer el push :). el 8 may. 2019 a las 10:42
  • He podido adaptarlo a mi código sin problemas. Muchas gracias!
    – David_helo
    el 8 may. 2019 a las 14:11

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.