Click here to Skip to main content
15,881,938 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm trying to send an array stringified from a hidden input created in js, but the value never goes to the formBuilder.

The code that creates the hidden input from a different js file is:

JavaScript
$(form).append(
        $('<input>')
            .attr('type', 'hidden')
            .attr('formControlName', 'selectedMessages')
            .attr('ng-reflect-name', 'selectedMessages')
            .val(JSON.stringify(selectedMessages))
    );

It creates perfectly the hidden input and the value too, but never sends the info, what can i do to solve this?

this is my component.ts:

JavaScript
import { Component, OnInit } from '@angular/core';
import { searchClient } from '../inicio/inicio.component';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { FirestoreService } from '../../services/firestore/firestore.service'
import "../../js/TableJS.js";


const index = searchClient.initIndex('messages');

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



export class InboxComponent implements OnInit {
  messagesForm: FormGroup;
  errorMessage: string = '';
  successMessage: string = '';
  constructor(
    private fb: FormBuilder,
    private fireservice: FirestoreService) {

   }

  ngOnInit() {
    this.createForm();
  }

  createForm() {
    this.messagesForm = this.fb.group({
      selectedMessages: ['']
    });
  }
  // t01jbCy7XDGdDuUoIWw9
  getUserByID(userId: string){
    this.fireservice.getUserByID(userId);
  }
  console(value){

      console.log(value);


  }
  getArchivedMessages(){

  }
  getInMessages(){

  }
  getOutMessages(){

  }
  deleteMessage(){

  }
  archiveMessage(){

  }
  checkForFullMsgDelete(){

  }

}


the input that creates when i click the button that calls the (form).append is:

HTML
input type="hidden" formcontrolname="selectedMessages" ng-reflect-name="selectedMessages" value="["selectedmessages values"]";


on submit i call console(messagesForm.value), and its always empty, not totally because it sends the empty variable "selectedMessages" from the formBuilder, but you know what i mean.

What I have tried:

I've tried FromArray, doing the submit after the input is created.. and more things i don't remember right now
Posted
Updated 29-Apr-20 21:00pm

1 solution

Html input elements require a name attribute in order to be transmitted on form submission. The framework fancy names won't cut it.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900