Search code examples
angularangular-servicesangular-forms

Submit form in angular 2+ with arrays in model


How can i create a Model with arrays

My Response:

 {
    "mail": "test@tes.com",
    "password": "test",
    "field_user_terms_and_conditions": {
        "und": {
            "value": "1"
        }
    },
    "profile_main":{
        "field_your_name":{
            "und":{
                "0":{
                    "value":"test"
                }
            }
        },
        "field_type_of_diet":{
            "und":{
                "value":"vegan"
            }
        }
    }
}

Then I need to create a model to send with submit form

My model:

export class UsuarioModel {

    mail: string;
    password: string;
    -- this is my problem field_user_terms_and_conditions: string;

}

My HTML

<input type="checkbox" name="terms" class="form-checkbox check-style"
                                        [(ngModel)]="usuario.field_user_terms_and_conditions">

Finally return a error in response... if I put my response in Postman, works fine..

Any idea how I can do it?

example in https://stackblitz.com/edit/angular-wnew3y

Thankssssss


Solution

  • Sorry, I not understood the way to do it... I've declarated my value in my model and then in my service, create my data so POST it.

    Example in my service:

        const authData = {
          returnSecureToken: true,
                profile_main: {
                    field_type_of_diet:{
                      und: {
                        value: usuario.type_of_diet
                      }
                    }
                }, .........
    
        };
    

    Thanks all for the patience, if I can help any user here for this post just tell me :)