Frontend not reflecting changes in Angular chatbot component

I’m working on an Angular 16 project with a chatbot component. I’ve added it to my app.component.html like this:

<my-chat-widget [isActive]="chatEnabled"></my-chat-widget>

The chat component has several input properties:

export class ChatWidgetComponent implements OnChanges {
    @Input() isActive = false;
    @Input() availableActions = [];
    @Input() chatHistory = [];
    @Input() isVisible = false;
    @Input() hasNewAlert = false;
}

I’m trying to update these properties from another component:

constructor(private chatWidget: ChatWidgetComponent) {
    this.chatWidget.hasNewAlert = true;
}

But the changes aren’t showing up in the UI. How can I make sure the frontend updates when I change these properties from a different component? Is there something I’m missing about how Angular handles updates between components?

hmm, that’s an interesting issue youre facing! have you considered using a shared service to manage the chatbot state? it could help with communication between components. also, are you using OnPush change detection? that might affect how updates are propogated. what other approaches have you tried so far?

hey owen, sounds like a tricky one! have u tried using ngOnChanges() in ur chat component? it detects input changes. also, make sure ur not mutating objects directly. use this.chatWidget = {…this.chatWidget, hasNewAlert: true} instead. that should trigger change detection. lmk if that helps!

Your approach of directly modifying the ChatWidgetComponent’s properties from another component isn’t ideal in Angular’s architecture. Instead, consider implementing a state management solution like NgRx or using a shared service to handle communication between components. This ensures proper change detection and maintains component encapsulation.

Alternatively, you could leverage Angular’s EventEmitter and @Output decorators to emit changes from the parent component. Then, handle these events in the ChatWidgetComponent to update its internal state. This method adheres to Angular’s unidirectional data flow principle and promotes better component design.

Remember to implement the OnChanges lifecycle hook in your ChatWidgetComponent to react to input changes effectively. This will help ensure your UI stays in sync with the component’s state.