Updating the value of an input decorator in Angular 14: A step-by-step guide

Having trouble changing the input array value in Angular 14. I'm not sure why it's not working. If anyone has any insights or solutions, please help!

Snippet from app.component.ts:

changeVal() {
      this.httpClient.get<string[]>('assets/role.json').subscribe((data) => {
      this.allData = data;
      console.log(data);
      });
   }

Check out the demo: https://stackblitz.com/edit/angular-ivy-3p6bxk?file=src%2Fapp%2Fauto%2Fauto.component.ts

Answer №1

That's why the ngOnChanges() life cycle hook exists!

When the value changes in the parent component, the child component is not automatically aware of it. This is where the OnChanges interface comes in handy. By implementing the ngOnChanges() method with a SimpleChanges parameter, we can ensure that the child component gets notified whenever the value changes in the parent component.

Check out this updated StackBlitz example to see it in action.

Happy coding!

Answer №2

Your code is functioning as intended.
Within your auto.component.ts, you are only assigning values to the rateData property during the component initialization. Any subsequent changes to your @Input() data will not affect the rateData property.

An easy solution would be to utilize a setter method.
You can achieve this by adding the following:

  @Input() set data (data: Array<string>) {
    this.rateData = data;
  }

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Eliminate the need for 'any' in TypeScript code by utilizing generics and partials to bind two parameters

I'm working with TypeScript and have the following code snippet: type SportJournal = { type: 'S', sport: boolean, id: string} type ArtJournal = { type: 'A', art: boolean, id: string} type Journal = SportJournal | ArtJournal; type J ...

Reactivity in Angular Autocomplete with API Integration

I went through all the tutorials on Angular Autocomplete using API to follow the steps. I implemented valueChanges to monitor the form control, used switchMap to send a new request with each keyword change, and then displayed the data in the autocomplete d ...

The RxJS scan operator has the ability to retrieve only the most recently pushed partial data

I am facing a challenge with managing a BehaviorSubject. My goal is to update it by passing a Partial<Type> and I have implemented the following code structure. The use of scan helps in merging new and old data seamlessly. personUpdates$ = new Behavi ...

Implementing ngClass change on click within an Angular component using Jasmine: A comprehensive guide

It seems like the click event is not triggering and the ngClass is not changing to active on the button I am attempting to click. -- HTML: <div class='btn-group' role='group' aria-label=""> <button type='button&apos ...

Angular 4 incorporating a customized Bootstrap 4 accordion menu for seamless navigation

I am trying to implement a nested menu using a JSON object in Angular 4. Below is the code I have written. <div id="panel-group"> <div class="panel panel-default" *ngFor="let mainItem of objectKeys(my_menu); let i = index"> <div class ...

Discover one among numerous interfaces available for handling Promise responses

My API handler returns a promise of a type. The object returned can be one of the following interfaces, depending on the API response: export interface Event { statusCode: number } export interface CreateEvent extends Event { data: Object } export in ...

Leveraging Javascript Modules within a Typescript Vue Application

The issue at hand I've encountered a problem while attempting to integrate https://github.com/moonwave99/fretboard.js into my Vue project. My initial approach involved importing the module into a component as shown below: <template> <div&g ...

Unwrapping the Angular ngForm Mystery: Resolving

I was attempting to retrieve values using ngOnInit and initializing the form with default values, but for some reason the form is returning as undefined. I also tried using patchValue, but since the form is undefined, it doesn't work. It's intere ...

Organize pairs of strings into a nested array within an array using Angular

Currently, I am working on a project in Angular. In this project, I have a string that contains special characters which I have successfully removed using regular expressions. Now, my goal is to arrange the first two strings within square brackets and the ...

TS2604: The JSX element '...' lacks any construct or call signatures and is unable to be processed

As part of our company's initiative to streamline development, I am working on creating a package that includes common components used across all projects. We primarily work with TypeScript, and I have successfully moved the code to a new project that ...

Cypress: Uncovering the method invoked by a button click

I'm currently utilizing Vue3 with Vite and Cypress. My Vue3 component utilizes the script setup SFC syntax. Below is the code snippet for my component: <template> <div> <button data-cy="testBtn" @click="btnClick()&q ...

Modify the arrow design for the expansion panel's default arrow style

Looking to customize the design of an angular expansion panel? Check out the images below for inspiration: Before Customization: https://i.sstatic.net/4u6NS.png After Customization (Not expanded): https://i.sstatic.net/8N6Br.png After Customization (E ...

Currency formatting in ionic2 is not working properly when tested on a

Formatting currency in Ionic2 framework can be done like this: {{finalPremium | currency : 'eur' : true :'.2-2' }}. Interestingly, this functionality only appears to function properly in the browser. When tested on an iPhone device, no ...

Using Angular 6 to invoke a service function within the ngOninit lifecycle hook

I am encountering an issue while trying to implement a service within the ngOnInit method. My goal is to utilize an SVG map where I access elements of the map using the following code: let a = document.getElementById("biharsvg") as HTMLObjectElement; T ...

Issues with Angular 2 template not refreshing during testing

I'm struggling to get this to function properly. it('should have the expected <h1> text', async(() => { let fixture = TestBed.createComponent(AppComponent); fixture.detectChanges(); const sectionEl = fixture.debugElement.que ...

Enhancing the design of the Mat Calendar with a stylish border

I've been attempting to put a border around my mat calendar, but I'm having trouble figuring out the specific class that will give me the exact look I want. I need it to be just like this: https://i.sstatic.net/fTGbp.png I tried using the follow ...

Unable to connect to 'formGroup' as it is not recognized as a valid attribute of 'form'. ("<div class="upload-right"><div class="signup">

In my Angular 7 project, I am struggling to identify where the error lies within this form. The error message keeps stating that 'form' is not a known property of this method. Can someone please point out where I went wrong in the following code ...

Concealing the BottomNavigation bar in Nativescript

Currently using Nativescript 7+ and seeking to implement a feature where the TabStrip can be hidden on certain pages post navigation. Below is the relevant section of my .html code. <BottomNavigation id="bottomNav"> <TabStrip> ...

How can we update the information in the initial observable using data from a separate observable, taking into consideration specific conditions in Angular with RxJS?

Encountered a scenario where I need to fetch data from an API (e.g. cars via getCars()) that returns Observables and then get additional data by car model (e.g. features via getFeatures(model)) in order to replace the features data for each car. In relati ...

What is the best way to utilize the async pipe along with @defer for efficiently loading and declaring variables in the template within Angular 17

One way I can accomplish this is by using @if. An example of this is: @if(items$ | async; as items), where I can assign the array of items to a variable named 'items' using the 'as' keyword in the template. Is there a similar approach ...