Component function gets executed twice in angular (2+)

4 views
Skip to first unread message

guy.wit...@gmail.com

unread,
Jan 16, 2018, 9:31:18 AM1/16/18
to Angular and AngularJS discussion
I have a component as follows:

import { Component, OnInit } from '@angular/core';

    @Component({
        selector: 'app',
        template: `
<div>
    <div>value of a :
        <input [(ngModel)]="a" />
    </div>
    <div>value of b :
        <input [(ngModel)]="b" />
    </div>
    <div>
        Value of a = {{a}}
        <br> Value of b = {{b}}
        <br> Sum instantaneous {{calc(a+b)}}
    </div>
</div>
`   
    })
    export class AppComponent implements OnInit {

        a: string = "";
        b: string = "";
        constructor() { }

        ngOnInit() {
        }

        calc() {
            console.log('test');
            return (+this.a + +this.b);
        }
    }

Every time, I modify (say, key press) anything in textboxes or even just focusing/defocusing textboxes, the 'calc' function gets executed twice.

What am I missing here or how can I get the function execute once (instead of twice).

Reply all
Reply to author
Forward
0 new messages