Why is my AngularJS li scope not being removed?

Can someone please assist me? I am new to AngularJS and need help with a function I created.

I have created a function that adds a next tab section and removes the current section. Similarly, I have another function that adds a previous tab section and removes the current one.

However, it seems like only the data is copied by this function and not removed or displayed.

Please lend me your expertise!

Here is my code:

HTML Code:

<body ng-app="taskPanel">
   <div class="" ng-controller="TasksController">
  <tabset panel-tabs="true" panel-class="panel-inverse">
       <tab>
                         <tab>
                            <tab-heading> <span class="hidden-xs">ACTIVE</span> <span class="badge badge-primary">{{tasksComplete.length}}</span></tab-heading>
                            <div class="tasklist">
                                <ol class="panel-tasks">
                                    <li ng-repeat="item in tasksComplete">
                                        <a href="#" class="preview-question">
                                            <i class="fa fa-eye">eye</i>
                                        </a>
                                        <label>
                                            <span class="task-description">{{item.title}}</span>
                                             <span class="label label-{{item.color || 'primary'}}" ng-show="item.label">{{item.label}}</span> 
                                             <div class="more-icn">
                                                <div class="pull-left">
                                                   <button  class="active-check" ng-click="pushActive(this, item)">Push to InActive Tab</button>
                                                </div>

                                             </div>
                                        </label>
                                    </li>
                                </ol>
                            </div>
                         </tab>
                         <tab>
                            <tab-heading> <span class="hidden-xs">InACTIVE</span> <span class="badge badge-primary">{{tasksComplete.length}}</span></tab-heading>
                            <div class="tasklist">
                                <ol class="panel-tasks">
                                    <li ng-repeat="item in tasksInComplete">
                                        <a href="#" class="preview-question">
                                            <i class="fa fa-eye"></i>
                                        </a>
                                        <label>
                                            <span class="task-description">{{item.title}}</span>
                                             <span class="label label-{{item.color || 'primary'}}" ng-show="item.label">{{item.label}}</span> 
                                             <div class="more-icn">
                                                <div class="pull-left">
                                                   <button  class="active-check" ng-click="pushInActive(this, item)">Push To Active Tab</button>
                                                </div>

                                             </div>
                                        </label>
                                    </li>
                                </ol>
                            </div>
                         </tab>

   </tabset>

   </div>
  </body>

AngularJS Code:

// Add your code here

var appan = angular.module('taskPanel', []);
appan.controller('TasksController', ['$scope', function($scope){
    $scope.tasksComplete = [
      { title: "I'm first" },
      { title: "I'm Second" },
      { title: "I'm third" }
    ];


    $scope.tasksInComplete = [
      {title: "I'm incomplete 1"},
      {title: "I'm incomplete 2"},
      {title: "I'm incomplete 3"}
      ];

      $scope.remove = function(scope){
        scope.remove();
      };


      $scope.pushActive = function(scope, item){
        $scope.tasksInComplete.push(item);
        scope.remove();
      };

      $scope.pushInactive = function(scope, item){
        $scope.tasksComplete.push(item);
        scope.remove();
      };

}]);

Check out the Live Demo

Answer №1

Here is one approach you can take:

Controller:

 $scope.completedTasks = [
   { name: "Task 1"},
   { name: "Task 2"},
   { name: "Task 3"}
 ];

 $scope.removeTask = function(index) {
   $scope.completedTasks.splice(index, 1);
 };

View:

ng-click="removeTask($index)"

Answer №2

Check out this solution using $index to remove elements from an array:

  $scope.pushActive = function(index, item){
    $scope.tasksComplete.splice(index, 1);
    $scope.tasksInComplete.push(item);

  };

  $scope.pushInActive = function(index, item){
    $scope.tasksInComplete.splice(index, 1);
    $scope.tasksComplete.push(item);

  };

You can see the working example on Plunker:

http://plnkr.co/edit/fp3gcf9PlBi9XnpNYFZQ?p=preview

Answer №3

I successfully debugged your code and the corrected version can be viewed here.

The wording in the original code was causing confusion for me. Once I aligned the View terminology with that of the controller, everything fell into place.

var appan = angular.module('taskPanel', []);
appan.controller('TasksController', ['$scope', function($scope){
  $scope.tasksInActive = [
    { title: "First Item" },
    { title: "Second Item" },
    { title: "Third Item" }
  ];

  $scope.tasksActive = [
    {title: "Uncompleted Task 1"},
    {title: "Uncompleted Task 2"},
    {title: "Uncompleted Task 3"}
  ];

  $scope.pushActive = function(scope, item){
    $scope.tasksInActive = removeItemFromList(item, $scope.tasksInActive);
    $scope.tasksActive.push(item);
  };

  $scope.pushInActive = function(scope, item){
    $scope.tasksActive = removeItemFromList(item, $scope.tasksActive);
    $scope.tasksInActive.push(item);
  };

  function removeItemFromList(item, list){
    return list.filter(function(i){
      return i != item;  
    });
  }
}]);

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 options in the dropdown menu vanish when interacting with a jQuery feature that relies on

Recently, I have found AJAX, JSON, and jQuery to be indispensable tools in my coding endeavors. The application I am working on is a replacement for a flawed one, and it is coming along nicely. Despite making progress with the AJAX method, I have encounte ...

What is the best way to list all registered states in ui-router?

Is there a method available to display a list of all the states that are registered when utilizing ui-router? ...

Having trouble transferring data between Vue.JS components

Wondering how to pass data from the parent component (Home route) to the child component (playlist route) using props? Encountering a "TypeError: Cannot read property 'length' of undefined" error in the child component? These two components are c ...

Invoke two functions simultaneously on a single Onchange event

Can someone help me understand how to trigger two functions by changing the value of a specific dropdown list using the "OnChange" event in Ajax? Note: The system typically only displays the output of the showhistory() function. Here is my existing code: ...

During the installation process of Next JS, I faced a challenge that hindered

While setting up NextJS, I ran into the following issue: D:\Codes\React\Learn>npx create-next-app npm WARN using --force Recommended protections disabled. npm WARN using --force Recommended protections disabled. npm ERR! code E404 npm ERR ...

Combining Laravel and vue.js for seamless file uploading functionality

Successfully uploading files using vue.js 1.0 with a POST request looks like: store () { this.storingMessage = true; var form = new FormData(); form.append('subject', this.message.subject); form.ap ...

Tips for concealing a product item when the price is listed as 0

I am facing an issue where some of my products have a price of 0. In order to address this problem, I would like to hide those products from the collection pages. My question is: How can I hide the .productItem or .ItemOrj if the .productPrice span is eq ...

Is there a way to make Bootstrap 5 load its jQuery plugins right away, rather than waiting for DOMContentLoaded?

After migrating to Bootstrap 5, I noticed that the following code is broken: <script src="https://code.jquery.com/jquery-3.6.0.js"></script> <script src="https://cdn.jsdelivr.net/npm/@popperjs/<a href="/cdn-cgi/l/email-prot ...

What is the process for importing something from index.js within the same directory?

My folder structure is similar to the one below /components/organisms -- ModuleA.vue -- ModuleB.vue -- index.js The content of index.js: export { default as ModuleA } from "./ModuleA.vue" export { default as ModuleB } from "./ModuleB.vue&qu ...

Adjust the burger menu color based on the background color of the section

Currently, I am working on creating a hamburger menu component with fixed positioning using the 'fixed' property. The menu icon consists of three white lines by default, but I want them to change to black when placed in a white section. I have tr ...

How to access global variables in node.js modules?

I'm looking to move some functionality to a new file called helpers.js. Below is the code that I have put in this file. How can I access the app variable within my method so that I can retrieve the config element called Path? Helpers = { fs: requ ...

What could be the reason that Ng Repeat fails to work when a button is triggered from a separate form

I have an HTML table that includes an ng repeat directive and two buttons. The first button opens a modal with a form to create a new user. When I click save, it adds the new user to the list. The second button is within the same form and also adds a user. ...

D3 Treemap for handling extensive sets of data

I am uncertain if there exists a method to incorporate the desired feature. I am seeking a solution similar to zoomable treemaps, but to initially load a limited number of child levels and then dynamically add more child nodes without requiring a node clic ...

The useNavigate() hook from react-router-dom is not properly setting the id in the URL path

I am currently using react-router-dom v6 for my routing needs. My goal is to pass an ID in the navigate URL path. Here is the onClick method and button code that I am working with: let navigate = useNavigate(); const routeChange = (id) => { let ...

I have a json file that I need to convert to a csv format

I am currently working with a json file, but I need to switch it out for a csv file. Do I have to use a specific library to accomplish this task, or can I simply modify the jQuery 'get' request from json to csv? I attempted the latter approach, b ...

Automatic suggestions for my personalized npm module (written in ES6/Babel) in Webstorm

When I utilize the material-ui package in Webstorm, I am able to experience helpful auto-completion using the ctrl+space shortcut: https://i.stack.imgur.com/Pivuw.png I speculated that this feature may be attributed to the inclusion of an index.es.js fil ...

Filtering tables in Angular 6 using checkbox options

I have a functional code snippet that filters a table using checkboxes. However, I am facing an issue when unchecking a checkbox. It does not return the original array as expected. .html <ng-container *ngFor="let group of groups"> <label cla ...

Angularjs provides support for dynamic dropdowns through its dynamic data

As a beginner in Angular, I'm looking to dynamically create a dropdown menu with three buttons (add, edit, delete) by clicking on a link that says "create dropdown button". It should be possible to create multiple dropdowns by clicking on an Add/Delet ...

Checking the response data using an Angular interceptor

As a beginner in AngularJS, I am looking for a way to intercept all $http requests and redirect to the login page if the response data indicates a 401 error. I would greatly appreciate any assistance with this issue. Thank you! ...

Why does Asp.net Webform load twice every time I click on the form link?

In my asp.net webform project, I am encountering an issue where the page is running twice when clicked. The problem arises when calling two functions (Fill DropDowns) on the Page_Load Event. During debugging, it was observed that the control enters the Pa ...