admin管理员组文章数量:1305842
I have an Angular component that receives an @Input() property from a parent component. The parent component fetches data asynchronously (from an API), and when the data is set, ngOnChanges should update my local component variables.
However, my variables remain null even after the data is received and ngOnChanges is triggered.
import { Component, Input, OnChanges, SimpleChanges } from '@angular/core';
@Component({
selector: 'app-invoice',
templateUrl: './invoiceponent.html',
styleUrls: ['./invoiceponent.css']
})
export class InvoiceComponent implements OnChanges {
invoiceId: string | null = null;
@Input()
invoiceData: any = null;
ngOnChanges(changes: SimpleChanges): void {
if (changes['invoiceData'] && changes['invoiceData'].currentValue) {
this.invoiceId = this.invoiceData?.invoiceGenInfo?.invoice?.id;
console.log("Updated invoiceData:", this.invoiceId); //this works
}
}
onEdit(): void {
console.log("Invoice Data in Edit:", this.invoiceId);
}
}
Above is the angular component of the code. The console.log in the ngOnChange method does print the value after a few seconds since it's asynchronous. But even after I see the console value when I trigger onEdit() with a click from a template the console logs as null.
How can I fix this? I need to update the template with the data from the invoiceData object after the API from the parent is completed.?
Thanks
I have an Angular component that receives an @Input() property from a parent component. The parent component fetches data asynchronously (from an API), and when the data is set, ngOnChanges should update my local component variables.
However, my variables remain null even after the data is received and ngOnChanges is triggered.
import { Component, Input, OnChanges, SimpleChanges } from '@angular/core';
@Component({
selector: 'app-invoice',
templateUrl: './invoiceponent.html',
styleUrls: ['./invoiceponent.css']
})
export class InvoiceComponent implements OnChanges {
invoiceId: string | null = null;
@Input()
invoiceData: any = null;
ngOnChanges(changes: SimpleChanges): void {
if (changes['invoiceData'] && changes['invoiceData'].currentValue) {
this.invoiceId = this.invoiceData?.invoiceGenInfo?.invoice?.id;
console.log("Updated invoiceData:", this.invoiceId); //this works
}
}
onEdit(): void {
console.log("Invoice Data in Edit:", this.invoiceId);
}
}
Above is the angular component of the code. The console.log in the ngOnChange method does print the value after a few seconds since it's asynchronous. But even after I see the console value when I trigger onEdit() with a click from a template the console logs as null.
How can I fix this? I need to update the template with the data from the invoiceData object after the API from the parent is completed.?
Thanks
Share Improve this question asked Feb 3 at 16:45 Abrew Abraham AlexAbrew Abraham Alex 456 bronze badges 1- angular version? – Naren Murali Commented Feb 3 at 16:51
1 Answer
Reset to default 2Instead of ngOnChanges
just compute the value you want using a get method, it is more reliable than ngOnChanges
.
You can also use ChangeDetectionStrategy.OnPush
for better performance.
import { Component, Input, OnChanges, SimpleChanges, ChangeDetectionStrategy } from '@angular/core';
@Component({
selector: 'app-invoice',
templateUrl: './invoiceponent.html',
styleUrls: ['./invoiceponent.css'],
// changeDetection: ChangeDetectionStrategy.OnPush,
})
export class InvoiceComponent implements OnChanges {
@Input()
invoiceData: any = null;
get invoiceId(): string | null {
return this.invoiceData?.invoiceGenInfo?.invoice?.id || null;
}
onEdit(): void {
console.log("Invoice Data in Edit:", this.invoiceId);
}
}
本文标签: javascriptAngular ngOnChanges Not Updating Component Variables When Input() ChangesStack Overflow
版权声明:本文标题:javascript - Angular ngOnChanges Not Updating Component Variables When @Input() Changes - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741808727a2398666.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论