Modifying the user interface (UI) through the storage of data in a class variable has proven to be

If I need to update my UI, I can directly pass the data like this:

Using HTML Template

<li *ngFor="let post of posts; let i = index;">
  {{i+1}}) {{post.name}}
<button (click)="editCategory(post)" class="btn btn-danger btn-sm">Edit</button>
</li>

Using Component TS

editCategory(post){
  post.name = "Something";
}

This method reflects the changes in the UI and works as expected. However, when trying to make changes indirectly, it doesn't work as follows:

Using HTML Template

<li *ngFor="let post of posts; let i = index;">
{{i+1}}) {{post.name}}
<button (click)="deleteCategory(post); modal1.show();" class="btn btn-danger btn-sm">Edit</button>
</li>
<div #modal1="bs-modal">
<button (click)="finallyDeleteCategory(); modal1.show();" class="btn btn-danger btn-sm">Edit</button> //making changes through this button
</div>

Using Component TS

deleteCategory(post){
  this.savedPost=post;
}

finallyDeleteCategory(){
  this.savedPost.name="deleted"; //this Doesn't work
}

Storing data in a class variable and then making changes to it does not reflect in the UI.

Questions:

  1. How do I store the reference of the Post in a class variable?
  2. Is this the correct way to make changes indirectly or should I pass on data in the HTML itself?

Any assistance would be greatly appreciated, thank you!

Answer №1

It is important to note that since finallyDeleteCategory is outside the loop context, passing a reference of post and changing its name directly will not work as it does not exist in that scope.

Attempting to use this.savedPost.name="deleted"; within the loop does not work due to the elements being part of the posts array. Instead, you can save the index i of the post object and utilize it in your method like:

(click)="editCategory(i)"

deleteCategory(loopindex){
 this.index=loopindex
}

In the finallyDeleteCategory method, you can retrieve the posts from the array using the saved index and then make changes to the post object directly.

finallyDeleteCategory(){
  posts[this.index].name="deleted";
}

Answer №2

Based on your current configuration, the deleted post will only be saved if you use the 'deleteCategory' method on the list item. In order for the changes to take effect immediately, you need to press the edit button after performing the deletion.

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

Exploring the variations between getRequestHandler and render functions in Custom Next.js applicationsIn a

Greetings, I found it quite unexpected that there is a lack of information available on the functionalities of the getRequestHandler and render functions within the next package. As I am in the process of setting up a custom server, I am curious about wh ...

Is it possible that an error is triggered when utilizing canActivate or canChildActivate?

A problem has arisen while using canActivateChild or canActivate in the child route. Despite working fine previously, an error is now being thrown: ERROR in src/app/app-routing.module.ts(8,7): error TS2322: Type '({ path: string; redirectTo: string; ...

After loading Google Maps, initiate an AJAX request

Is there a way to determine if Google Maps has finished loading? I need to send an Ajax request once the map is fully loaded. Currently, I am displaying a group of users on a map with info windows. However, the browser becomes unresponsive due to the larg ...

What are the strategies for distinguishing between languages in React Native prior to mounting the component?

I had high hopes for this solution, but unfortunately it doesn't work as expected. The issue is that this.text.pupil is undefined. Could the problem possibly be related to componentWillMount? If so, how can I handle multiple languages outside of ...

"Looking to display an image object retrieved from AWS S3 using a signed URL in Vue.js? Here's how you

<div v-for="(data, key) in imgURL" :key="key"> <img :src= "getLink(data)" /> </div> imgURL here is an array that contains file names. methods: { async getLink(url){ let response = await PostsService.downloadURL({ i ...

Express.js allows for AJAX POST requests without the use of newlines

My current approach to sending the request is as follows: $(document).on('submit', 'form', function(e) { var form = e.currentTarget; console.log($(this).serialize()); $.ajax({ url: form.action, ...

Protractor: Unable to reach variables within closure set in the parent function

I have a code snippet in one of my JS files that looks like this: // test/lib/UserHelper.js 'use strict'; var Firebase = require('firebase'); exports.createUser = function (email, password) { browser.executeAsyncScript(function (do ...

Utilizing Angular to import an SVG file from a backend and incorporate its content as a template

I am looking for a solution to load an SVG date from my Spring Boot backend and utilize it as an Angular template. Currently, the request is structured like this: getSVG (): Observable <any> { return this.http.get(`${environment.apiUrl}/path ...

Is there a way to display the button just once in a map function while still retaining access to the iteration value?

Utilizing formik within material-ui for a form has been quite productive. I've implemented a map function to print text fields and managed to display the Submit button only once by targeting the index. However, there's an issue when attempting to ...

Tips for emphasizing a cube face when it is hovered over in THREE.js

I'm currently using a raycaster to determine the face of a cube and then apply color to it following this method: const colorAttribute = intersected.object.geometry.getAttribute('color'); colorAttribute.setXYZ(face.a, color.r, color.g, color ...

Refreshing SQL Server data using an HTML form

In the table below: <table id="skuTable" role="grid"> <thead> <th class="skuRow">Order</th> <th>Fab. Date</th> <th class="skuRow">Norder</th> <th>Color</th> ...

How can I make multiple elements change color when hovering with CSS?

Hey there! I have a little design challenge that I need help with. On my website (), I am trying to make multiple elements of a specific item change color when the user hovers over the text. If you hover over the "more" menu item, you'll notice that o ...

Is the next function triggered only after the iframe has finished loading?

First and foremost, I understand the importance of running things asynchronously whenever possible. In my code, there exists a function known as wrap: This function essentially loads the current page within an iframe. This is necessary to ensure that Jav ...

How to determine button placement based on the content present on the page

I'm struggling to find the right CSS positioning for a button on my page. I want the button to stay fixed in a specific location, but when there's a lot of content on the page, I need it to adjust its position accordingly. Initially, I want the ...

Problem with Angular Material Sidenav Styling

Currently, I am working with the mat-sideNav route and encountered a CSS issue with the sidenav after selecting a new route. When I click on a new route using the sidenav module and then return to the sidenav to change routes again, I notice that the hover ...

What is the best way to stop navigation when a button is clicked inside a table row that contains a routerLink?

Looking for a solution with an angular material table set up like this: <table mat-table [dataSource]="myTable" matSort> <ng-container matColumnDef="column1"> <th mat-header-cell *matHeaderCellDef>My Column</th> <td ma ...

Opening a Text File via an ASPX Web Page

Currently, I am immersed in a Web Pages project. The goal is to retrieve text from a Text File and display it within a div element. To achieve this, I utilized the ajax xmlhttprequest method, following a tutorial available at http://www.w3schools.com/ajax/ ...

Getting the value of a hidden input field within a div by accessing the event.target that initiated the event in JavaScript

I am working with a set of li elements, each containing specific child elements structured like this: <li class="list"> <!--Parent--> <input type="hidden" id="id" value="3"> <!--Child 1--> <div class="cd-1">....</div ...

Collecting Images Based on Quantity

Despite using async to download URIs on every request and closing when a certain count is reached, I am still encountering the issue of files not being downloaded properly and exiting before reaching their maximum. Can someone suggest the best solution t ...

Executing a webservice method in an html page using javascript without the need to refresh the page

Is it possible to call a webservice from an index.html page using JavaScript? My webservice is located at "localhost/ws/service.asmx" and the specific web method I want to call is called HelloWorld. The index.html page contains an HTML submit button whic ...