Challenge faced: Angular array variable not refreshing

I am currently working on a map application where users can input coordinates (latitude and longitude). I want to add a marker to the map when the "Add Waypoint" button is clicked, but nothing happens. Strangely, entering the values manually into the .ts file works and updates when the button is pressed.

Here is the content of my main.component.ts file:

import { Component, OnInit } from '@angular/core';
import { ThemePalette } from '@angular/material/core';
import { ProgressBarMode } from '@angular/material/progress-bar';
import { Waypoint } from '../models/Waypoints';

@Component({
  selector: 'app-main',
  templateUrl: './main.component.html',
  styleUrls: ['./main.component.scss']
})

export class MainComponent implements OnInit {
  mode: ProgressBarMode = 'determinate';
  color: ThemePalette = 'primary'

  public latValue = 38.267730;
  public lonValue = -110.720120;

  constructor() { }

  waypoints: Waypoint[];

  addWaypoint() {
    this.waypoints.push(
      {
        lat: this.latValue,
        lon: this.lonValue
      }
    )
  }

  ngOnInit(): void {
  }

}

This is how my main.component.html file looks like:

<mat-grid-list cols = "8" rowHeight = 350px>
    <mat-grid-tile colspan = "4" rowspan="2">
        <mat-card class = "colspan-4 rowspan-2">
            <mat-card-title>Waypoints</mat-card-title>
            <mat-card-content>
                <mat-grid-list cols = "3">
                    <mat-grid-tile>
                        <mat-form-field class="example-form-field" appearance="fill">
                            <mat-label>Latitude</mat-label>
                            <input matInput type="text" [(ngModel)]="latValue">
                            <button *ngIf="latValue" matSuffix mat-icon-button aria-label="Clear" (click)="latValue=0">
                              <mat-icon>close</mat-icon>
                            </button>
                        </mat-form-field>     
                    </mat-grid-tile>
                    <mat-grid-tile>
                        <mat-form-field class="example-form-field" appearance="fill">
                            <mat-label>Longitude</mat-label>
                            <input matInput type="text" [(ngModel)]="lonValue">
                            <button *ngIf="lonValue" matSuffix mat-icon-button aria-label="Clear" (click)="lonValue=0">
                              <mat-icon>close</mat-icon>
                            </button>
                        </mat-form-field>                          
                    </mat-grid-tile>
                    <mat-grid-tile>
                        <button mat-button color="primary" (click)="addWaypoint()">Add Waypoint</button>
                    </mat-grid-tile>
                    <mat-grid-tile colspan = "3" rowspan = "2">
                        <mat-list>
                            <div mat-subheader>Waypoints</div>
                            <mat-list-item *ngFor="let waypoint of waypoints; let i = index">
                                <mat-icon mat-list-icon>place</mat-icon>
                                <div mat-line>Waypoint {{i}}</div>
                                <div mat-line>{{waypoint.lat}}, {{waypoint.lon}}</div>
                            </mat-list-item>
                        </mat-list>
                    </mat-grid-tile>
                </mat-grid-list>
            </mat-card-content>
        </mat-card>
    </mat-grid-tile>
</mat-grid-list>

The contents of my Waypoints.ts file are as follows:

export class Waypoint {
    lat:number;
    lon:number;
}

If I use hardcoded data, here is what my addWaypoint function looks like:

addWaypoint() {
    this.waypoints = [
      {
        lat: 38.267730,
        lon: -110.720120
      },
      {
        lat: 40.267730,
        lon: -112.720120
      }
    ]
}

Answer №1

By default, Angular tracks changes by reference. Merely pushing data onto an array is not sufficient. To trigger change detection, you must either assign the array to a new one, modify the default strategy for change detection, or manually instruct Angular to detect changes.

const updatedWaypoints = [...this.waypoints];
updatedWaypoints.push(...);
this.waypoints = updatedWaypoints;

For more information, refer to the documentation: https://angular.io/api/core/ChangeDetectorRef#usage-notes

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

Issue with React useCallback not being triggered upon a change in its dependencies

useCallback seems to be capturing the wrong value of its dependency each time. const [state, setState] = React.useState(0); const callback = React.useCallback(() => { console.log(state); // always prints 0, why? }, [state]); React.useEffec ...

Warning: Potential spacing issues when dynamically adjusting Material UI Grid using Typescript

When working with Typescript, I encountered an error related to spacing values: TS2322: Type 'number' is not assignable to type 'boolean | 7 | 2 | 10 | 1 | 3 | 4 | 5 | 6 | 8 | "auto" | 9 | 11 | 12'. No lint errors found Version: typesc ...

The Angular framework is unable to locate a differ that supports the object '[object Object]' which is of type 'object'

In my Angular project, I am calling an API and receiving the following JSON data: { "id": 16, "poste": "ameur taboui", "desciption": "f", "service": "f", ...

Can you identify the specific function type passed through props?

interface IProps { handleCloseModal: React.MouseEventHandler<HTMLButtonElement> returnFunction: () => void; } export default function Modal({ children, returnFunction, handleCloseModal, }: React.PropsWithChildren<IProps>) { cons ...

React and TypeScript warns about possible undefined object

certificate?: any; <S.Contents>{certificate[0]}</S.Contents> <S.Contents>{certificate[1]}</S.Contents> <S.Contents>{certificate[3]}</S.Contents> If I set the type of props to `any` and use it as an index of an array, e ...

What is the best way to ensure a div is always scrollable?

I am currently working with Angular 4 and Bootstrap 4 (alpha 6). I have implemented ngx-infinte-scroll to fetch data from the server when the user scrolls to the top or bottom of the div. The issue I am facing is that the user can only scroll when there ar ...

What is the reason behind Angular 2's choice to implement the .ts file extension?

What is the significance of using the .ts file extension in Angular 2? ...

"Implementing an Angular route component that adjusts based on specific

I am currently working on routing within my application for the 'feed' module. Within this feed, there are two types of posts that I need to display with a direct link to show full information. How can I ensure that the right component is opened ...

How to apply custom styling to a specific element within an Angular Material 2 component using CSS based on its Id or

My EntryComponent features a material button menu where I attempted to customize the default style using ::ng-deep. However, the changes affected all button components in the parent Component as well. <style> ::ng-deep .mat-button{ max-width: 50 ...

Angular form grouping radio buttons in a unique way

Encountering some unusual behavior with radio buttons and forms currently. In my form, I have 4 radio buttons set up. The issue I'm facing is that when I click on a radio button, it remains enabled permanently. This means I can have all 4 options sel ...

What is the best way to convert a JSON string received from Angular into a Java Object within a Spring

I am currently utilizing WebSocket to create a chat application. Below is the code from my Angular application that sends a MessageModel object to the backend after converting it into a JSON string: sendMessage(message: MessageModel){ let data = JSON.str ...

Talebook by Syncfusion

I'm completely new to Storybook and I am currently exploring the possibility of creating a Storybook application that showcases a variety of controls, including Syncfusion controls and other custom controls that I will be developing in the future. Ha ...

When using TypeORM's save() method with an array of objects, the @PrimaryColumn() annotations are ignored, resulting

My current situation involves an entity called Point: @Entity() export class Point { @PrimaryGeneratedColumn('uuid') id: string; @IsUUID() @PrimaryColumn({ type: 'uuid', ...

Exploring the Various Path Options in Angular 2 Routing

As a newcomer to Angular and Node JS, I am currently working on an application and struggling with how to efficiently navigate between my different components. Users can input the name of a user and add books associated with them When clicking on a book ...

Firebase cloud function encountered an issue: Error: EISDIR - attempting to perform an unauthorized operation on a directory

I am currently working on a task that involves downloading an image from a URL and then uploading it to my Firebase cloud storage. Below is the code I have implemented for this process. import * as functions from 'firebase-functions'; import * a ...

Error: Attempting to access the value property of a null object within a React Form is not possible

I am currently developing a form that includes an HTML input field allowing only numbers or letters to be entered. The abbreviated version of my state interface is outlined below: interface State { location: string; startDate: Date; } To initiali ...

What could be causing my project to install an erroneous Angular version?

It appears that my project is not utilizing the latest Angular code, but I am unsure of the reason behind this. I have checked my package.json file and found the following dependencies: "devDependencies": { "@angular/common": "2.0.0", "@angular ...

Outside the observable in Angular, there is a vacant array

Within my Angular application, I am facing an issue with the following code snippet located inside a method: let cardArray: string[] = []; this.cardService.getCards().subscribe((cards) => { console.log("1", cards); for (const card of car ...

In order to address the issue of displaying a 404 error in In-Memory Angular,

I have watched all the videos regarding the In-memory web API and diligently followed all the steps and instructions. However, I am still encountering a 404 Error. Please inform me if I missed something or made an error. I have attempted to troubleshoot an ...

Creating a stepper module in Angular 6

Looking for assistance from Angular experts app.component.html <app-stepper [activeStep]="0"> <app-step [sid]="0"> <div>iam step 1</div> </app-step> <app-step [sid]="1"> <div>iam step 1& ...