Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

156
Visualizações
Use .innerHTML to define a input field with ng-model

I want to create multiple input fields in a for-loop and assign a unique ng-model name to each field so I can use the values later. Here is how I did it. inputmap is a map.

<script cam-script type="text/form-script">
for (let i=0;i<3;i++){
   var name = "inputmap.item"+ i.toString(); // inputmap.item0, inputmap.item1, inputmap.item2, etc
   document.getElementById("inputs").innerHTML += "<input type='text' ng-model='" + name + "' required/><br>" 
}
</script>

<div id="inputs"></div>

But when I look at the $scope.inputmap, it's empty.

When I hardcode the input fields like:

<div id="inputs">
   <input type='text' ng-model='inputmap.item0' required/><br>
   <input type='text' ng-model='inputmap.item1' required/><br>
   <input type='text' ng-model='inputmap.item2' required/><br>
</div>

I was able to get the values I entered in the fields. I am new to HTML and any help/hints is appreciated.

about 4 years ago · Santiago Gelvez
1 Respostas
Responde à pergunta

0

First you have += on document.getElementById("inputs").innerHTML += "<input type='text' ng-model='" + name + "' required/><br>" and can be changed to just =

There are to ways to do this. I think if you change document.getElementById("inputs").innerHTML += "<input type='text' ng-model='" + name + "' required/><br>" to document.getElementById("inputs").innerHTML = `<input type='text' ng-model="${name}" required/><br>` then that will add it in correctly. Never done this though and might not work where you can do my second way of doing it:

for(let i = 0; i < 3; i++) {
   let input = document.createElement('input') //Creates input element
   let wrapper = document.getElementById('inputs') //Gets the div element 
   let name = `inputmap.item${i.toString()}` //Gets the name

   input.setAttribute('ng-model', name) // Sets 'ng-model' attribute to name
   wrapper.appendChild(input) //Sets 'wrapper' as 'input' parent 
}

Hope this helps!

about 4 years ago · Santiago Gelvez Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda