in Education by
I have a mat-table with a component app-order-statuses on every row. the component calls the statuses of an order from my server. however when I load the table, the endpoint is called on every row. import { Component, Input, OnInit } from '@angular/core'; import { ApiService } from '../../../Services/api-service/api.service'; @Component({ selector: 'app-order-statuses', templateUrl: './order-statuses.component.html', styleUrls: ['./order-statuses.component.scss'] }) export class OrderStatusesComponent implements OnInit { @Input() order_id: any; public statuses: []; public isVisible: boolean = true; constructor(private ApiService: ApiService) { } ngOnInit() { let url = 'orders/' + this.order_id + '/statuses'; this.ApiService.get(url).subscribe((response: any) => { this.statuses = response.data.order_statuses; this.isVisible = false; }); } } how can I make it that it calls the endpoint only on an event for example (button click). JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
That's because you've made the API call in the ngOnInit. This lifecycle hook gets called every time there's a change in the @Input property of the Component. Just move the code to another method and call this method on the click of the button: import { Component, Input, OnInit } from '@angular/core'; import { ApiService } from '../../../Services/api-service/api.service'; @Component({ selector: 'app-order-statuses', templateUrl: './order-statuses.component.html', styleUrls: ['./order-statuses.component.scss'] }) export class OrderStatusesComponent implements OnInit { @Input() order_id: any; public statuses: []; public isVisible: boolean = true; constructor(private ApiService: ApiService) {} ngOnInit() {} getStatus() { let url = 'orders/' + this.order_id + '/statuses'; this.ApiService.get(url).subscribe((response: any) => { this.statuses = response.data.order_statuses; this.isVisible = false; }); } } And in your Template:

Related questions

0 votes
    I have these routes /items/:id /items /items/:id/edit ... etc I want to put the component, to ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 3, 2022 in Education by JackTerrance
0 votes
    I have an angular 7 project and I'm using Angular universal. Everything is working fine except when I ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 30, 2022 in Education by JackTerrance
0 votes
    I have these routes /items/:id /items /items/:id/edit ... etc I want to put the component, to ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 26, 2022 in Education by JackTerrance
0 votes
    I have a simple submit method, where Console.log(11) is printed first then console.log(1). I need ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 22, 2022 in Education by JackTerrance
0 votes
    I have this template that I used everywhere in my project : {{ event.title }} {{ event.content } ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 19, 2022 in Education by JackTerrance
0 votes
    I have a simple submit method, where Console.log(11) is printed first then console.log(1). I need ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 19, 2022 in Education by JackTerrance
0 votes
    I create an angular library "mylib" I create a service which uses BehaviorSubject Observable from rxjs. For ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    I'm using Angular Material in my application. I have input fields like this: E-mail Which produces a ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    I'm using Angular Material in my application. I have input fields like this: E-mail Which produces a ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    I have this template that I used everywhere in my project : {{ event.title }} {{ event.content } ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 1, 2022 in Education by JackTerrance
0 votes
    I need to create an new link from the current route: https://website.com/post/99 = curent browser ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 24, 2022 in Education by JackTerrance
0 votes
    I need to create an new link from the current route: https://website.com/post/99 = curent browser ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 23, 2022 in Education by JackTerrance
0 votes
    I have a json file contains a icons object like this: "icons" : { "logo" : "fa fa-caret-down ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 9, 2022 in Education by JackTerrance
0 votes
    I'm writing here since I have a question about Mat Toolbar and Mat-sidenav from Angular Material. I' ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 14, 2022 in Education by JackTerrance
0 votes
    so with ng2 rc.6 I am getting an error of: Uncaught (in promise) Error: (SystemJS) SyntaxError: ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 8, 2022 in Education by JackTerrance
...