1

I have simple code where I display the value of an input in HTML template from an array of data that I have in Typescript. I ran into a problem, where the data wasn't loaded fast enough and I was getting errors until the data was loaded. I fixed that by checking if the endTime or StartTime are empty. while trying to fix the problem I've noticed that my console.log is printing many times. I expected that this function would run once. Why is it running many times? Am I doing something wrong? Or is this how angular is supposed to work?

To give more information this is HTML code.

I have multiple cards that have 2 input value that call my function returnTime()

<mat-card *ngFor="let item of requestVehicles.controls; let pointIndex=index" [formGroupName]="pointIndex">

The function is called like this

<input [value]="returnTime(pointIndex, true)">

The actual function

  returnTime(pointIndex, isEnd) {
    if (this.data.isEdit) {
      console.log('ENDTIME =======' + this.endTime + '          STARTTIME =========' + this.startTime);
      // rest of the logic that should not be relevant.
    } else {
      return '';
    }
  }

the maximum value of pointIndex is usually 2-3. so that should not explain the amount of console logs.

enter image description here

9
  • 1
    To start with, value attributes are worthless in Angular. And to answer you, functions, getters and variables in the template are checked several times by Angular to detect changes. Just by moving your mouse around, you will see that it triggers them several times. It's usually a bad practice to bind functions to inputs, they should be bound only to outputs (events). Commented Nov 14, 2018 at 8:46
  • then how do I give value to an input? I want the previous value to appear when I edit, instead of creating new. Commented Nov 14, 2018 at 9:07
  • An input should be bound with a ngModel or formControl attribute : if you want to give it a value, give a value to that model/form control. Commented Nov 14, 2018 at 9:09
  • I can't use any of them. I am using 2 inputs for 1 formControl. Date and time, the date input is bound by using formControl, but the time I manually take the value and change the time. Thats because angular material datepicker doesn't let me change time. Commented Nov 14, 2018 at 9:15
  • 1
    Yeah well then that's bad code for bad design. Instead of doing weird things, consider making a custom form control through ControlValueAccessor that will handle both date & time. Commented Nov 14, 2018 at 9:16

1 Answer 1

1

Following my comments :

You want a datepicker that handles both date & time. Because of that, you create two inputs that are both bound to a single form control / ngModel.

This is bad design : Angualr doesn't that possibility into account.

Instead of doing that, you should create a new, custom form control implementing ControlValueAccessor.

You can find the documentation here : https://angular.io/api/forms/ControlValueAccessor

And several tutorials can be found online, for instance :

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.