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

128
Visualizações
how to load dynamic data in HTML appended div using Angular

this is my html file, i.e. app.component.html

<button mat-raised-button color="primary" 
        mat-button class="nextButton" 
        (click)="calculatePremium()">
  Calculate
</button>


<div id="calculatedData">
                                    
</div>

the above button is used to calculate the data by calling an api, and I'm getting the expected output, but when this button is clicked I want to show that generate value inside the div mentioned above, but that dynamic value is not showing in the browser, and the value is shown as undefined.

and this is my app.component.ts file

calculatePremium(){
    console.log("calculating");
    let frmData: any = this.form.value;
    this.dobVal = frmData.contact.dob;
    let onlyYear = String(this.dobVal).split(' ')[3];
     
    // AGE    
    let age = (Number(new Date().getFullYear()) - Number(onlyYear));
        

    // NAME
    let name = frmData.contact.firstName;    
    

    // GENDER    
    let gender = frmData.contact.gender;    


    // smoking    
    let smoker = frmData.basicInfo.isSmoker;    


    // no of dependents    
    let dependentsCount = Number(frmData.basicInfo.adults) + Number(frmData.basicInfo.children);    
    

    let finalObj = {
        'name': name,      
        'age': age,      
        'gender': gender,      
        'smoking': smoker,      
        'dependent': dependentsCount
      }    

    this.calcualte.calculatePremium(finalObj).subscribe(
      (data: any) => {
        //success        
        console.log(data);        
        Swal.fire('Plan Amount', 'is  ' + data.name + data.amount, 'success');        
        this.amount = data.amount;        
        console.log(this.amount);        
        
        //alert('success');     
      },      
      (error) => {        
        //error        
        console.log(error);      
      });    
      console.log(this.amount);        
      const x = document.getElementById('calculatedData');  
  This is where I want to show the dynamic value, but the amount is shown as undefined when 
  rendered on the browser.
      x.innerHTML = `<div class="appendme">${this.amount}</div>`;    
      const len = x.getElementsByTagName('div').length;    
      console.log(len);  
  }} 
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

If you're starting Angular, please go through their short tutorial. It introduces all the basics.

In your case you'd need to use *ngIf to check if the amount variable is defined and interpolation to render the value.

<div *ngIf="!!amount">
  {{ amount }}                                    
</div>
about 4 years ago · Juan Pablo Isaza 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