What is the best way to remove a Firebase Object by its specific Property:key value?

As I delve into AngularJs and Firebase, I am working on a todo list application.

In my application, there is an HTML form structured like this:

<form name="frm" ng-submit="addTodo()" id="form-border">
    <div class="form-inline">
      <input type="text" class="form-control" name="newtodo" ng-model="newtodo" required="" id="inputform" />
      <button class="btn btn-success" ng-disabled="frm.$invalid">Go</button>
      <button class="btn btn-danger" ng-click="clearCompleted(todo)">Clear Completed</button>
    </div> <!-- end form group -->
  </form>

Below the form are the actual tasks labeled as "todos" in an unordered list format, added through the form above.

Here's an example of the code:

<li ng-repeat="todo in todos" ng-mouseenter="hover = true" ng-mouseleave="hover = false" class="list-group-item" ng-class="inactive" >
      <input type="checkbox" ng-model="todo.done" ng-click="clickDone(todo)"/>
      <span ng-class="{'done':todo.done}" id="donetask">{{todo.title}}</span><!-- add line-through when task is done-->
      <ng-include ng-class="{'hiRank-off': !hover}" src="'rankbutton.html'"></ng-include><!-- ng-include rankbutton-->
    </li>

The data in my app.js is stored with Firebase:

var myData = new Firebase("https://firebaseio-demo.com/ToDos");

and

$scope.todos.$add({'title':$scope.newtodo,'done':false, 'timetag': datecreated}); //push to Array 

    $scope.newtodo = '';

To mark my tasks completed using clickDone(todo), I access the variable 'todo' from my ng-repeat as follows:

$scope.clickDone = function(todo){
    $scope.todos.$save(todo); //saves todo state when checked
  };  

I'm facing a challenge with deleting objects using ng-click="clearCompleted(todo)", which aims to remove items marked as complete. This functionality is mentioned in the first code block However, it seems that clearCompleted()function is not within the ng-repeat="todo in todos" scope, causing difficulties in deleting objects with this function.

I attempted to delete without success, resulting in errors such as:

TypeError: Cannot read property 'done' of undefined

$scope.clearCompleted = function($scope.todos){
    $scope.todos.$remove($scope.todo.done);
  };

Answer №1

Eureka! The solution finally came to me! All I had to do was use the forEach() method to scan through $scope.todos and verify if todo.done equated to true.

 $scope.clearCompleted = function(){ $scope.todos.forEach(function(todo){ if(todo.done){ $scope.todos.$remove(todo); } }); };

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

React input value doesn't get updated on onChange event causing the input

Currently, I'm working on a React application that requires a table with inputs in each row. To achieve this, I am utilizing Material-UI and Formik. However, I've encountered a bug where input fields lose focus whenever I type something into them ...

Dealing with images on my live React application

For managing static images in my react app, I have integrated cloudinary as a CDN service. Can anyone suggest a seamless way to switch between using local image folders during development and switching to the CDN URL for production efficiently? ...

Creating a custom JavaScript clock based on stored database values

I am looking to create an analog clock using JavaScript. I have both working hours and off hours stored in a database. My goal is to display working hours in one color and off hours in another color on the clock. How can I achieve this? The database prov ...

Unable to make API call within ReactJs Component due to technical issues

I am brand new to react and I am coming across a few things that I don't quite understand (even after researching). So, I have decided to ask my questions here. Beforehand, I just want to apologize for my poor English and any potentially silly questio ...

Is it possible for Angular and Flux to collaborate harmoniously?

Flux is a unique unidirectional data flow concept developed by the React team, offering various benefits such as Undo/Redo functionality, ease of testing, maintaining a single app state, and more. Exploring the idea of integrating Flux with AngularJs could ...

Encountered an issue when attempting to transfer my code from CircleCI to Firebase

This is my first time using CircleCI, so I followed a tutorial here. I am using it to build, test, and deploy my Angular app to Firebase. I have searched for solutions but couldn't find anything. I attempted to change the node version without success ...

Discovering XMLHttpRequest Issues within a Chrome Application

Is there a way to identify XMLHttpRequest errors specifically in Chrome App? For instance, I need to be able to recognize when net::ERR_NAME_NOT_RESOLVED occurs in order to display an error message to the user. While XMLHttpRequest.onerror is activated, ...

Angular module now equipped with web worker for independent operation

My goal is to create a standalone AngularJS module that utilizes web workers for handling heavier processing tasks. This module will be integrated into an Angular web application that I am currently developing, as well as potentially other projects. I env ...

What is the best way to transmit the attributes of an object with a defined state in one React component to another component using a link and display them?

I currently have two components and one route in my project. I am trying to pass the properties of the place object from one component to another so that I can display them accordingly. The first component holds the list of places in its state, where I map ...

Struggling to make the upvoting feature function properly in the Thinkster MEAN Stack Tutorial

While following the MEAN Stack tutorial on this website, I decided to modify my code to utilize Controller as instead of using $scope as demonstrated in their examples. I am encountering an issue with the upvote functionality. Whenever I click to upvote a ...

Event of Mouse Wheel - Speed/Impact

Exploring a new THREE.JS project, I am implementing a mousewheel scroll event to transition from 0 to 1 smoothly. Nevertheless, my goal is to replicate the momentum effect seen on this website playdoh by merci Michael So far, here's what I have acco ...

Utilizing Office.js: Incorporating Angular CLI to Call a Function in a Generated Function-File

After using angular-cli to create a new project, I integrated ng-office-ui-fabric and its dependencies. I included in index.html, added polyfills to angular.json, and everything seemed to be working smoothly. When testing the add-in in Word, the taskpane ...

Issue arises when the function in a for loop disrupts the loop from functioning correctly

This particular script is designed to scan through a sheet for currency values and create key/value pairs for a JSON based on the column and row headers. It basically detects a currency value, then traces back row and column numbers until it reaches a non- ...

Retrieving cookie from chrome.webRequest.onBeforeSendHeaders

I have been developing a Firefox add-on with the goal of intercepting HTTP requests and extracting the cookie information. While I was successful in extracting the 'User-agent' data from the header, I faced difficulties when attempting to extract ...

How do you modify the up vector of the camera in three.js?

When working with three.js, I encountered a situation where I set the camera's focal point using camera.lookAt(0, 0, 0), but the camera's up vector ended up pointing in a random direction. I have a specific up vector in mind that I want the camer ...

Tips for changing the "Height map image link" in a Mesh.CreateGroundFromHeightMap on babylonjs

I recently started using babylon.js and am experimenting with the CreateGroundFromHeightMap function. I want to change the URL for the Height Map parameter in babylon.js but I'm unsure how to do so. Here is the code I have written to create it: var ...

issues encountered when trying to integrate bootstrap.js in Django framework

My website is built using the Django templating engine with Bootstrap for design and basic JavaScript. While the CSS components from Bootstrap are working fine, I'm having trouble getting the JavaScript to work: {% load staticfiles %} <!d ...

Having trouble getting Emberjs to properly handle an Ajax POST request

Currently, my goal is to send data to the database using Ajax POST. To start off, I have an HTML form as follows: <script type="text/x-handlebars" id="project"> <div class="row"> <div class="span6"> <form class ...

What steps should I take to repair this array table?

I've created a simple array table that uses 3 user-defined values to determine the value of a variable. Although I've written similar code in the past, I'm having trouble figuring out why this one isn't working. The table is quite large ...

This hyperlink is not redirecting to an external website

I recently downloaded a one pager template that has a unique design. All the hyperlinks within the template are set to link to specific sections on the page and use a data-link="" parameter. However, I encountered an issue when trying to link one of the m ...