AngularJS experiencing issues with deleting function implementation

Here is my JavaScript code:

camListApp.controller("Hello", function($scope, $http, $uibModal){

    $scope.del=function(data){

        var result=confirm('are you sure?');
        if(result==true){
        var index=getSelectedIndex(data);
        $scope.records.splice(index, 1);
        }
        };
        function getSelectedIndex(data) {
        for (var i =0; i<$scope.records.length; i++)
        if($scope.records[i].data==data)
        return i;
        return -1;
        }

This is the HTML code snippet:

  <td><button class="btn" ng-click="del(record.filename)">Delete</button></td>

This is my JSON data:

[{"cameraid":"000000001","timestamp":"2016-07-09 10:06","filename":"c037731fc2256177ba29c7893caacf04","locationid":"Bedok01"}   
{"cameraid":"000000003","timestamp":"2016-07-13 11:35","filename":"4fd2413d30073b4b6a5cacbb8b7c1965","locationid":"Bedok01"} 
{"cameraid":"000000003","timestamp":"2016-07-13 14:41","filename":"6b6b62948eb679efeb650d609c85b7aa","locationid":"Bedok01"}

I am trying to implement a delete function in AngularJS that will remove a record when a button is clicked. I also want MongoDB to delete the corresponding data simultaneously. Any assistance on how I can achieve this?

Answer №1

To start, the first step is to establish an endpoint on your server. MongoDB serves as a database that can be accessed from your backend by setting up a controller or another similar method. Once this is completed, you can initiate an API call to the designated endpoint.

 $scope.remove = function(data) {
       $http.post('/items/' + data.id + '/remove')
        .then(function(){
           .... include the remainder of your AngularJS code here
       })
   };

Answer №2

Perform an HTTP request to delete data from the database and then remove the specified object from the list by using the splice method as illustrated below:

<td><button class="btn" ng-click="removeItem(item)">Delete</button></td>

$scope.removeItem = function(item) {
var index = $scope.items.indexOf(item);
$scope.items.splice(index , 1);
    $scope.updateList();
};

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

The server's file URLs are modified within the page source of a WordPress site

I've been attempting to integrate Adsense code into a WordPress blog at demonuts.com. I placed the Google code in the TEXT WIDGET provided by WordPress. However, upon running the website, I noticed that the URLs for .js, .css, or .png files are being ...

When the enter key is pressed in a contenteditable div, line breaks are disregarded

Is there a way to ensure that when the return key is pressed to start a new line for a post, the output actually starts on a new line? I've been having trouble with this and would like to know the most common solution. Check out the demo below for te ...

Send user a notification upon detecting changes in the database - using JavaScript and AJAX!

Hey there! I'm diving into the world of JavaScript and AJAX for the first time, and I could really use some guidance on a particular issue. I'm looking to implement a feature where the user is notified whenever there is a change in a value of a ...

Issues with the initial calculator project I built using JavaScript (excluding HTML and CSS)

My first calculator is nearly complete, but I have encountered a challenge. The functionality of my calculator is quite simple; it prompts the user for input using window.prompt and performs addition, subtraction, multiplication, or division based on the u ...

The border of the Material UI Toggle Button is not appearing

There seems to be an issue with the left border not appearing in the toggle bar below that I created using MuiToggleButton. Any idea what could be causing this? Thank you in advance. view image here view image here Just a note: it works correctly in the ...

Exploring the potential of nested arrays within checkbox inputs

I'm struggling with extracting a nested array from an input value of a checkbox. How should I handle this situation? Here are the values: const othersOptions = [ {procedure:'ORAL PROPHYLAXIS',price: 1000}, {procedure:'TOOTH RESTORATION ...

Simple method for converting pixel values to em units

I'm searching for a simple method to incorporate a line of code into one of my plugins in order to convert specific pixel values into em values. My project's layout requires the use of ems, and I'd prefer not to rely on external plugins for ...

Simple HTML files on a local server are having trouble loading images and JavaScript, yet the CSS is loading

Having worked in web design for quite some time, I recently began working on a basic Angular app that is meant to be very simple. For this project, I am only using the angular files and a single html file as the foundation. As I started setting up the bas ...

Unable to sort the list items when utilizing the load() function

I have multiple li elements within a ul and I am using the following code to sort them in ascending order based on the data-percentage attribute: $(function() { $(".alll li").sort(sort_li).appendTo('.alll'); function sort_li(a, b) { re ...

Error message encountered: 'GlobalStyles' is not a valid export from the module '@material-ui/system' (referenced as 'SystemGlobalStyles')

./node_modules/@material-ui/core/GlobalStyles/GlobalStyles.js Error encountered while trying to import: 'GlobalStyles' is not exported from '@material-ui/system' (imported as 'SystemGlobalStyles'). I am currently grappling wi ...

Several functions linked to a single prop in Vue

The reference material can be found here: https://v2.vuejs.org/v2/guide/components-custom-events.html#Customizing-Component-v-model seems a bit confusing to me regarding how to link multiple events to the same component: In: https://codesandbox.io/s/da ...

Angular 2: Harnessing the power of Observables with multiple Events or Event Handlers

In the component template, I have grouped multiple Inputs and their events like this: <tr (input)="onSearchObjectChange($event)"> <th><input [(ngModel)]="searchObject.prop1"></th> <th><input [(ngModel)]="searchObje ...

A controller in Angular.js that leverages several different services

I'm having trouble injecting multiple services into a controller. Here are my listed files: index.html file: <script src="'./angular/angular.min.js'></script> <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.0-beta ...

Select state and city options similar to the national breakdown page

I'm attempting to replicate a state and city selection box similar to the one on the NTTS Breakdown website. You can see it in action here: When you select a state on the left side of the webpage, the city selection box displays "loading data" and th ...

Solving the challenge of converting images to text using react-native and the @react-native-ml-kit/text-recognition package

As I work on my react native project, I have encountered a challenge. I am trying to implement a feature that allows users to either take a photo or select one from their library. Once the image is chosen, I want to extract all the text from it and display ...

Enhancing a node.js application with express()

I am currently utilizing Express MVC with node.js. The primary object I am working with is express(), which is assigned to an alias called app: var express = require('express'); app = express(); Is there a way for me to expand the functionali ...

Phonegap and the Importance of HTTP Requests

Having an unusual issue with Phonegap where my XMLHttpRequests are firing twice. Currently, I am working on an app that involves using XMLHttpRequests to generate a dynamic list of events for users to choose from. In addition to this, I am utilizing jQuery ...

Ways to maintain uniform size in displaying images even when they are of different dimensions

Within my web application, administrators have the ability to upload various images that will be displayed on the site. The challenge arises from the fact that these images come in different sizes and dimensions. However, it is important that all users w ...

Check for a rapid return if the function ends up returning null in JavaScript

Is there a way to make this code more concise? const result = getResult(); if (!result) { return; } // Work with result I have several instances of this code in my project and I'm looking for a simpler solution like: const result = getResult() ...

Storing an array of JSON objects in separate rows within an SQL database table

I need help with inserting data from an API post request into a MySQL table using Express JS. The JSON array contains multiple objects that I want to fill out the rows in the table, but I can't seem to get it right. Any assistance would be appreciated ...