The sum is being treated as a concatenation instead of an addition in this case

Why is the somma value showing the concatenation of totaleEnergetico and totaleStrutturale instead of a sum?

RiepilogoCombinatoStComponent.ts

export class RiepilogoCombinatoStComponent implements OnInit {

  constructor() { }

  interventi: AssociazioneInterventoSt[] = []
  arr: number = 0
  selezione: number

  totaleStrutturale: number = 0
  totaleEnergetico: number = 0
  somma: number = 0

  ngOnInit() {
    this.interventi = window.history.state.interventi
    console.log(this.interventi)
  }

  calcola() {
    let totaleSt: number
    this.interventi.forEach(c => {
      this.totaleEnergetico = this.selezione
      totaleSt = c.prezzoSupInt
      this.totaleStrutturale = totaleSt
    })
    this.somma = this.totaleEnergetico + this.totaleStrutturale
    
  }

AssociazioneInterventoSt.ts

prezzoSupInt: number

RiepilogoCombinatoStComponent.html

<div class="container">
    <div class="card my-5" *ngFor="let el of interventi; index as j">
        <div class="card-header" style="text-align: center;">
            <div [(ngModel)]="arr" name="fieldName" ngDefaultControl>
                INTERVENTO: {{el.intervento.codice}}
            </div>
        </div>
    
        <div class="card-body">
            <div *ngIf="interventi[arr]">
                <select class="form-control" [(ngModel)]="selezione">
                    <option [value]="int.prezzoVista" *ngFor="let int of interventi[arr].associazione; index as i">
                        {{int.intervento.codice}}
                    </option>
                </select>
                <div style="text-align: center; margin-top: 10px;">
                    <button class="btn btn-outline-default" (click)="calcola()">Get result</button>
                </div>
                <hr>
                Structural Cost: {{el.prezzoSupInt}}<br>
                Energy Cost: {{totaleEnergetico}}
                <hr>
                Total: {{somma}}<br>
                Integrated Cost:<br>
                <hr>
                Savings in euro:<br>
                Savings %:<br>
                <hr>
                Safety equipment: 
            </div> 
        </div>
    </div>    
</div>

Answer №1

See if you can modify this:

this.result = this.firstValue + this.secondValue;

Into this:

this.result = (+this.firstValue) + (+this.secondValue);

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Translate data from two "contact form 7" date fields into JavaScript/jQuery to display the date range between them

NEW UPDATE: I was able to get it working, but the code is a bit messy. If anyone can help me clean it up, I would greatly appreciate it. <div class="column one-fourth" id="date-start">[date* date-start date-format:dd/mm/yy min:today+1days placeholde ...

Utilize jQuery to choose a specific tab

I have implemented a jquery tab in my UI. I want to have the second tab selected when the page loads, instead of defaulting to the tab with the "Active" class. Here is my HTML tab code: <div class="typo"> <div class="container-fluid"> ...

An error stating that "DataTable is not a recognized function" occurred within the document function

Previously, I set up datatables using the code below: $(function () { $('#keywords-table').DataTable({ "ajax": ({ url: "{{ route('getKeywordsByProductId') }}", method: "get", ...

What are the steps to utilize kendo-fileselect in order to upload files from an Angular application to a C# web API?

We are integrating Kendo for Angular into our current project. In our existing system, we utilize kendo-upload which triggers a server call immediately. However, we cannot follow the same approach for this particular page. https://i.stack.imgur.com/qdn2b. ...

Are you experiencing issues with the .submit() function when used in conjunction with other

Currently, I am working on a survey form that incorporates JQuery to dynamically display or hide fields based on user selections. //FORM CONDITIONALS: This script is responsible for hiding all the helpfulness radios and only displaying them when "Yes" fo ...

Generate a hard copy of the data table row without needing to view it beforehand

Here are some records from a table: +--------+---------+-------+----------+--------+ | Name | Address | Phone + Email | Action | +--------+---------+-------+----------+--------+ | Andy | NYC | 555 | <a href="/cdn-cgi/l/email-protection" cl ...

The impact of redefining TypeScript constructor parameter properties when inheriting classes

Exploring the inner workings of TypeScript from a more theoretical perspective. Referencing this particular discussion and drawing from personal experiences, it appears that there are two distinct methods for handling constructor parameter properties when ...

Retrieve the file name from the URL while disregarding the query string

Looking for a way to extract the test.jsp from this URL: http://www.xyz.com/a/test.jsp?a=b&c=d Can anyone provide guidance on how to accomplish this task? ...

Using iScroll without animation by using scrolltoelement on an iPhone PhoneGap application

I've been working on an iOS PhoneGap app that uses Backbone and some other libraries. To handle scrolling, I implemented iScroll which has been functioning properly. However, I encountered a problem with the following code snippet: events: { &apo ...

Implementing server authentication for page requests in a nodeJS and angularJS application

My application relies on passport.js for authentication. One of my main requirements is to prevent access to a specific page (e.g., '/restricted') for users who are not logged in. Currently, anyone can directly access the "localhost:3000/#/restr ...

Utilizing a personalized component with a mat-autocomplete feature within reactive forms by nesting

I'm struggling to add a nested component to the parent form group when the component only contains a mat-autocomplete. I attempted the same method used for the address subform which worked well, but for the user search, it's required to pass in t ...

Transitioning one element's opacity to zero while concurrently increasing another element's opacity to replace it

There are a total of 3 div elements and 3 links included in this setup. The goal is to only display one div at any given time. Upon clicking on a link corresponding to a different div, the current one should fade out while the selected one fades in seamle ...

The modal window pops up immediately upon the first click

Experience a dynamic modal element that springs to life with just the click of a button or an image. The magic lies in the combination of HTML, CSS, and jQuery code: <div id="modal-1" class="modal"> <div class="button modal-button" data-butto ...

Is it possible to determine if jQuery find returns true or false?

Snippet of HTML Code <div class="container1"> <div class="box1">Box 1</div> <div class="box2">Box 2</div> <div class="box3">Box 3</div> </div> <div clas ...

Tips on efficiently reusing a variable

As someone who is relatively new to Jquery and Javascript, I have been attempting to search for a solution to my question on this platform. However, it seems that the terminology I am using may not be accurate enough to yield relevant results. If there is ...

Executing a Parent Function from a Child Component in Angular 11

I have successfully passed values between Angular components using Input/Output several times before, but I am currently facing a unique situation for which I cannot find a solution: There are two components involved: home.component (parent component) T ...

Once the form is submitted, Vue automatically resets all the data

export default { data() { return { usrName: null, pass1: null, pass2: null, regState: {stateCode:-1}, } }, methods: { register: function () { this.axios.post("/login/", { baseURL: 'http://127 ...

Creating a list of identical elements with shared attribute values using nightwatch.js or JavaScript - a step-by-step guide

I have been using nightwatch.js for automating tests on a web application, and I am facing difficulties in creating a list of elements that share common values in their attributes. Below is an example: The first three spans with a common value for the att ...

Create a composition of several debounce promises in JavaScript

I am looking for a way to efficiently manage multiple costly server calls by continuously invoking a function that accepts a key and returns a promise containing an object. This object is guaranteed to have the requested key along with additional values, i ...

Adjust the colors of the borders upon clicking the button

Hello, I'm attempting to create a function where clicking on one button changes its border color to blue and changes the border color of all other buttons to orange. However, I'm encountering an issue where the border color for the other buttons ...