Angular and the dynamic transformation of the DOM tree through an array

I am using an HTML component within Angular. The template code looks like this:

              <tr ng-repeat="item in documentList track by $index">
                <td>{{item.TYPE}}</td>
                <td>{{item.DATE_CREATE}}</td>
                <td>
                  <a type="button" class="btn btn-default" ng-click="delDocument(item)">Delete</a>
                </td>
              </tr>

The method delDocument(item) is as follows:

  $scope.delDocument = function(item){
    if(confirm('Do you really want to delete the document?')) {
      $.ajax('/ajax/del_document.php', {
        'method': 'POST',
        'data': {
          'id': item.ID,
        },
        'success': function(){
          i = 0;
          while(i<$scope.documentList.length) {
            if($scope.documentList[i].ID===item.ID) {
              $scope.documentList.slice(i, 1);
            }
            i++;
          }
        }
      });
    }
  }

When a user clicks the Delete button, I expected that the element would be removed from the back-end via AJAX, the documentList would change, and Angular would update the DOM. However, despite the documentList changing, the HTML page remains unchanged. Can anyone help me diagnose what might be causing this issue? What am I missing?

Answer №1

Be sure to include $scope.$apply() within your success function once the operation is completed.

 $scope.delDocument = function(item){
    if(confirm('Are you sure you want to delete this document?')) {
      $.ajax('/ajax/del_document.php', {
        'method': 'POST',
        'data': {
          'id': item.ID,
        },
        'success': function(){
          i = 0;
          while(i<$scope.documentList.length) {
            if($scope.documentList[i].ID===item.ID) {
              $scope.documentList.slice(i, 1);
            }
            i++;
          }
          $scope.$apply();
          // Alternatively, use this safer way to apply $scope.$apply();
          // if($scope.$$phase){
            // $scope.$apply();
          // }
        }
      });
    }
  }

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

Sending information to @select of multiselect in Vue.js - vue-multiselect

I'm currently working on passing a parameter to the @select event function in Vue.js HTML <template> <div class="container"> <div v-for="(billItem, k) in billItems" :key="k" > <div class=&q ...

How can you determine the number of items in an object using

This is my object https://i.sstatic.net/28ibB.png I have been trying to display the number of images in this array, but for some reason it's not working and driving me crazy! <ion-item ng-click="openModal(value.$id)" ng-repeat="value in item ...

Could anyone else be having issues with the Mongoose function findByIdAndRemove() not functioning properly?

Upon selecting the checkbox using a /POST method to communicate with the server, it sends back the unique identifier (ID) of the item to the script. However, when attempting to execute the findByIdAndRemove() function, it appears that the document is not ...

Generate downloadable files dynamically in response to a POST request

I'm exploring the idea of creating a file without actually storing it on the drive, just for the purpose of immediate download within a POST request. const specialRequests = async (req, res, next) => { // POST request ... // processing let ...

Interact with CakePHP using onKeyUp event

Hey there, I have a quick and easy question. I'm working with a textbox that needs to trigger a javascript/jquery function whenever it is typed into. <?= $this->Form->input('contract_prices.'.$num.'.quantity', [ 'id ...

Is it necessary to have a strong understanding of JavaScript in order to effectively utilize jQuery?

While I currently do not have knowledge of JavaScript, I am intending to acquire it soon. My query is whether a strong grasp of JavaScript is necessary to utilize jQuery effectively. I am already familiar with ActionScript and PHP, which share certain si ...

Display the source code of an HTML element when clicked

Is there a way to show the source code of an element on a webpage in a text box when that specific element is clicked? I am wondering if it is feasible to retrieve the source code of an element upon clicking (utilizing the onClick property), and subseque ...

What is the best way to incorporate a transition on transform using Styled-components?

I attempted to add a transition on the transform property, but unfortunately, it did not produce any visible changes. I tested other properties such as: background-color, color... and they worked perfectly fine. live code: source code: // styled-compo ...

Exploring Java's options for XML parsing: Dom versus SAX Parser

Here is an example of XML data: <Books> <Book id="1" Name="C#"> <URL>http://localhost/download/M1.xml</URL> </Book> <Book id="2" Name="Oracle"> <URL>http://localhost/download/M2.xml</URL> </Book> < ...

Do the Push Notification APIs in Chrome and Firefox OS follow the same set of guidelines and standards?

Do Chrome and Firefox OS both use Push Notifications APIs that adhere to the same standard? If not, is either one moving towards standardization? ...

Extract the content from a widget on a different site

How can I eliminate the specified text shown in the image? https://i.sstatic.net/174fD.png This widget is sourced from an external website. Is it possible to use javascript to wrap a around the text and then remove it using css? It's important to ...

Creating a Custom FlatList Content Container with React Native

Is it possible to customize FlatList items with a custom component? I want to create a setup where my FlatList items are encapsulated within a custom component similar to the following: <ScrollView pt={8} px={16} pb={128} > <Card e ...

What is the best way to open Chrome Developer Console using Selenium?

While conducting automated tests with C#, Webdriver, and Chrome, I was exploring the use of the performance.timing.domComplete JavaScript console function through the chrome web developer tool. Here is an example of how I attempted to incorporate it into m ...

Unable to iterate through a collection of items

Struggling to incorporate the worklog JSON fields into an array using Python because I can't seem to loop over an array in Python. Take a look at the code snippet below: try: worklogs_to_insert = [] for i in issue.fields.worklog["worklogs"]: ...

Setting a dynamic default value for a Combobox using React Widgets

Currently delving into the world of javascript, I am working on creating a web client that showcases data from a database. Utilizing react.js and integrating react-widgets for some user-friendly widgets. One widget in particular, the combobox, pulls its da ...

Exploring the async/await functionality in TypeScript

Hey there! I'm working with this Angular/TypeScript service, and I've run into an issue where the query part of the function is asynchronous. This means that the return statement gets executed before the data is actually retrieved from the serve ...

Straight pipe made with tubegeometry

Is it possible to create a straight tube using TubeGeometry? All the examples I find only show how to make curved tubes. ...

Calculate a fresh set of whole numbers by summing up neighboring elements within a list using C++

As someone new to C++, I have a question regarding computing a new list by adding 8 consecutive elements and then dividing them by the number of elements added in a list with C++. Let's say we have a list called a[] with 200 elements, and we want to c ...

Guide on configuring browser compatibility for a NetBeans JSP application

While developing a JSP web application, I encountered an issue with certain buttons and labels not being visible in Internet Explorer but working fine in Google Chrome. How can I ensure that my application is compatible with all browsers for optimal user ...

Error Alert: Unresponsive JQuery Script

Currently, I am in the process of developing a website that requires a significant amount of script work. However, we are experiencing browser hang-ups and unresponsive script errors due to the extensive data load while loading listings for YouTube. The d ...