Tips for updating a selected value without altering the reference value

Encountering an issue with the angular framework when trying to select a value from ng-for loop

<tr *ngFor="let dept of department" (click)="clinicChoose(dept)">
  <td class="text-center">{{dept.sectionCode}}</td>
  <td>{{dept.sectionName}}</td>
  <td class="text-center" [style.color]="dept.flagStatus == 0 ? 'blue' : 'red'"></td>
</tr>

The clinicChoose function looks like this:

 clinicChoose(dept: CSection) {
    this.clinic = dept
  }

Whenever I update the value in this.clinic like this.clinic.flagStatus == 1, the selected value in the table changes as well. I want to prevent this behavior, so what can I do?

Clicking on a row in the red table should display data in the green table https://i.sstatic.net/jOLia.jpg

But why does the selected data in the table change when I update something?

https://i.sstatic.net/iwIsy.jpg

Answer №1

To ensure no changes are made to the original department, it is recommended to perform a deep cloning before assignment. Consider implementing a function or utilizing a library that can clone objects effectively, then assign it to

this.hospital = deepClone(department)
. By doing so, this.hospital will be an independent object, separate from the original list, preventing any unintended modifications.

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

Encountering a TypeError while attempting to construct and launch an IOS simulator through Cordova

Currently, I am in the process of developing an Ionic app that is designed for cross-platform functionality. Encountering an Error when attempting to build and run an IOS simulator using Cordova TypeError [ERR_INVALID_ARG_TYPE]: The "code" argum ...

Angular can be used to compare two arrays and display the matching values in a table

Having two arrays of objects, I attempted to compare them and display the matching values in a table. While looping through both arrays and comparing them by Id, I was able to find three matches. However, when trying to display these values in a table, onl ...

Stay updated with Angular/RxJS by subscribing to keys and observables!

While going through the documentation for Angular and RxJS, I couldn't find an answer to this specific query. When I make a HttpClient request in Angular and it returns an observable, like so: let request = this.http.get(url); request.subscribe( { ...

Steps to make ng-packagr detect a Typescript type definition

Ever since the upgrade to Typescript 4.4.2 (which was necessary for supporting Angular 13), it appears that the require syntax is no longer compatible. Now, it seems like I have to use this alternative syntax instead: import * as d3ContextMenu from ' ...

The 'type' property is not present in the 'ChartComponent' type, however, it is necessary in the 'ApexChart' type

Encountered an error highlighted in the title: Property 'type' is missing in type 'ChartComponent' but required in type 'ApexChart'. Any attempt to resolve this issue led to another error message: Type '{ type: string; ...

Tips for cycling through the backend API map reaction in Angular or Typescript

When I make a call to an API, it returns a response in the form of a map: {thomas: 3, test70: 2, tim: 2, elin: 2, sumeet12: 1} I tried iterating over this response in Angular, but encountered an error. Error Encountered: This expression is not callab ...

Merging RXJS observable outputs into a single result

In my database, I have two nodes set up: users: {user1: {uid: 'user1', name: "John"}, user2: {uid: 'user2', name: "Mario"}} homework: {user1: {homeworkAnswer: "Sample answer"}} Some users may or may not have homework assigned to them. ...

Is it not possible to access the width and height of an element using ViewChild, unlike what is suggested in other responses

I've encountered an issue with my Angular component. The HTML code for the component, before any TypeScript is applied, looks like this: <svg #linechart id="chartSpace"></svg> Wanting to create a responsive webpage featuring a line chart ...

Why aren't Dependencies like RxJS being installed from package.json in the Dockerfile?

As I try to containerize my application for deployment on GCP, I have encountered an issue during local testing. It seems that the rxjs module (and possibly others) listed in package.json is not being installed properly. When building my Dockerfile, I rec ...

How to extract and compare elements from an array using Typescript in Angular 6

I have created a new Angular component with the following code: import { Component, OnInit } from '@angular/core'; import { ActivatedRoute } from '@angular/router'; import { HttpClient } from '@angular/common/http'; @Compone ...

Generic type input being accepted

I am trying to work with a basic generic class: export class MyType<T>{} In the directive class, I want to create an @Input field that should be of type MyType: @Input field MyType<>; However, my code editor is showing an error that MyType& ...

Encountering compilation errors with Angular project following installation of a package

I recently installed the LocalStorage Package and encountered the following message: npm notice created a lockfile as package-lock.json. You should commit this file. npm WARN @angular/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfem ...

Is it possible to dynamically adjust the selectivity of function parameters?

// To start with, I aim to adjust the selectivity of the current function parameter based on a specific generic parameter type mentioned previously. My idea involves utilizing parameter sets and tuples. // However, implementing parameter sets to dynamical ...

Exploring Angular 2's @Input and @Output Directives

I'm unsure about whether I should be using @Input and @Output. From my understanding, these decorators are typically used when you want to establish communication between a parent and child component. Can you confirm or correct me on this? I have 3 c ...

Passing a type argument to the custom Toolbar of MUI DataGrid in TypeScript React: A step-by-step guide

Utilizing a personalized Toolbar alongside the Material UI DataGrid, I am transferring a set of props through object syntax: import { DataGrid } from "@mui/x-data-grid"; import InternalToolbar from "../InternalToolbar"; <DataGrid ...

Issue encountered with Angular Material's md-chips and md-autocomplete: The error message "control.registerOnChange is not a

When attempting to wrap mat-chips and mat-autocomplete into a ControlValueAccessor, I encountered the following errors: InfoEditorComponent.html:17 ERROR TypeError: control.registerOnChange is not a function at setUpModelChangePipeline (forms.js:2701) ...

Limit the category to a specific subset of strings

Looking for a way to implement literal type restrictions without using type aliases: const foo = (a: 'string', b: 'string') => { } foo("123", "abc") // should fail foo("123" as 'string', "abc" as 'string') I pr ...

Issue with Typescript flow analysis when using a dictionary of functions with type dependency on the key

I am utilizing TypeScript version 4.6 Within my project, there exists a type named FooterFormElement, which contains a discriminant property labeled as type type FooterFormElement = {type:"number",...}|{type:"button",...}; To create instances of these el ...

Comparing the use of Angular with Node.js and Nginx

Simply a question on my mind. In terms of benefits, which would be more advantageous: running my angular application through node with nginx as a reverse proxy or serving it directly from nginx? My intuition tells me that direct server from nginx would r ...

Is Redux or Flux the default state management tool used in React?

After using npx create-react-app my-app --template typescript to create a new React app, what is the default software architecture (MVC, Redux, or Flux)? I've been researching the differences between them and it has left me feeling a bit confused. I w ...