Angular @Input binding using function calling multiple times

Anuj TBE

I'm using Angular 8.

I have a child component with one @Input() property. This input has to be modified before binding and thus using method to return the data for binding like

<app-child [info]="getInfo()"></app-child>

And in the parent component, the getInfo() returns value like

getInfo(): any|null {
  console.log('called...');

  if (this.data) {
    return JSON.parse(this.data);
  }

  return null;
}

But this way, the method is called again and again whenever some event occurs in the child component.

Stackblitz example: https://stackblitz.com/edit/angular-child-com

Produce issue:

The form is rendered from the child component which accepts input from a method as described above.

Click on any button or input field and you can see the console log printing string from the method call with each event.

This is resulting in multiple times OnChange event trigger in the child components.

Lars Gyrup Brink Nielsen

You should retrieve the data and store it in a property in the parent component that you then pass to data binding with the child component. Angular will take care of the change detection

@Component({
  selector: 'app-parent',
  template: '<app-child [info]="info"></app-child>',
})
export class ParentComponent implements OnInit {
  info;

  constructor(private service: SomeDataservice) {}

  ngOnInit() {
    this.info = this.service.getData();
  }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Avoid calling function multiple times in angular

From Dev

Calling async function multiple times

From Dev

Calling onmouseover function multiple times

From Dev

Why is Angular input [value] = "function()" running multiple times?

From Dev

Binding Input to Angular controller function

From Dev

Avoid numbers incrementing multiple times when calling a function multiple times

From Dev

Multiple times method calling from angular template

From Dev

Click event is calling multiple times within function

From Dev

Calling the same function multiple times with different arguments?

From Dev

calling function multiple times with new results

From Dev

how to prevent calling a function multiple times in Jquery

From Dev

Calling the same function multiple times in select

From Dev

React onSetstate function is calling multiple times

From Dev

Calling function multiple times inside while loop

From Dev

Testing a Function by Passing Input Multiple Times

From Dev

Avoid evaluating the function with same input multiple times

From Dev

C++, repeat an input for a function multiple times

From Dev

Passing the output of a function back to the input multiple times

From Dev

jquery on change and input function run multiple times

From Dev

Validation using if statement using input binding in Angular

From Dev

Show multiple Matplotlib figures by calling a function multiple times?

From Dev

Using user input from a scanner multiple times?

From Dev

Angular Validator function called multiple times

From Dev

Angular - function in ngFor called multiple times

From Dev

NSNotificationCenter is calling multiple times

From Dev

Why my subject observable subscriber is calling multiple times in angular?

From Dev

Calling angular2 route guard multiple times

From Dev

Angular 2 material mat-select calling multiple times

From Dev

app.use ("*") seems to be calling function multiple times

Related Related

  1. 1

    Avoid calling function multiple times in angular

  2. 2

    Calling async function multiple times

  3. 3

    Calling onmouseover function multiple times

  4. 4

    Why is Angular input [value] = "function()" running multiple times?

  5. 5

    Binding Input to Angular controller function

  6. 6

    Avoid numbers incrementing multiple times when calling a function multiple times

  7. 7

    Multiple times method calling from angular template

  8. 8

    Click event is calling multiple times within function

  9. 9

    Calling the same function multiple times with different arguments?

  10. 10

    calling function multiple times with new results

  11. 11

    how to prevent calling a function multiple times in Jquery

  12. 12

    Calling the same function multiple times in select

  13. 13

    React onSetstate function is calling multiple times

  14. 14

    Calling function multiple times inside while loop

  15. 15

    Testing a Function by Passing Input Multiple Times

  16. 16

    Avoid evaluating the function with same input multiple times

  17. 17

    C++, repeat an input for a function multiple times

  18. 18

    Passing the output of a function back to the input multiple times

  19. 19

    jquery on change and input function run multiple times

  20. 20

    Validation using if statement using input binding in Angular

  21. 21

    Show multiple Matplotlib figures by calling a function multiple times?

  22. 22

    Using user input from a scanner multiple times?

  23. 23

    Angular Validator function called multiple times

  24. 24

    Angular - function in ngFor called multiple times

  25. 25

    NSNotificationCenter is calling multiple times

  26. 26

    Why my subject observable subscriber is calling multiple times in angular?

  27. 27

    Calling angular2 route guard multiple times

  28. 28

    Angular 2 material mat-select calling multiple times

  29. 29

    app.use ("*") seems to be calling function multiple times

HotTag

Archive