Generate an array of checked inputs to be used when posting to a REST API

I have been using .push() to create a checked array of "List" inputs for posting to a REST API. However, it doesn't seem to be working correctly.

When unchecking an item, it is not automatically removed from the array. Does anyone have a better solution? Please help me out! Thanks

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

Answer №1

Here is a possible solution, although it may not be the optimal one:

 $scope.$watch('lists', function(lists){
    $scope.count = 0;
    angular.forEach(lists, function(list){
      if(list.checked){
        $scope.count += 1;
        if (inputsList.indexOf(list.id) == -1) {
            inputsList.push(list.id);
        };
      } else {
          inputsList.pop(list.id);
      }
    })
  }, true);

Using a similar approach with some modifications:

index.html (incorporated ng-click)

<input type="checkbox" name="list_id[]" ng-model="list.checked" value="{{list.id}}" ng-click='updateItem(list)' />

app.js (eliminated $scope.$watch and made changes)

$scope.currentSelectedItem = [];      
$scope.updateItem = function(item) {
    if(item.checked) {
        $scope.currentSelectedItem.push(item);
    } else {
        $scope.currentSelectedItem.pop(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

Adjusting attention to the switched element

I am currently developing an inline editing feature using AngularJS. Here is the code snippet from my Controller: $scope.toggleEditor = function(){ $scope.editorEnabled = !$scope.editorEnabled; //toggle editor view //struggling with this part $time ...

We are unable to retrieve file content through Django's restful API

I have integrated Django restful API with AngularJS. Now, I am trying to upload files to the server using AngularJS and Django RESTful API. On the front-end, the HTML code is: <input type="file" data-upload-file id="uploadInputFile"> The AngularJS ...

When adding margin-left and margin-right, images do not appear in their designated positions

I have a chart displaying images, which are showing up correctly. However, I am facing an issue when I try to add some spacing to the chart by using margin-left and margin-right. Here is the CSS code I included: #chart1 { margin: 0 auto; ...

"Unlocking the power of AngularJS translate: A step-by-step

I'm seeking answers to two questions. 1) How can I utilize angularjs translate with ng-repeat? Although my Json file works fine, the text does not display when using ng-repeat. Here is a snippet from my json: "rules":{ "points":[ {"t ...

Is it possible to expand the capabilities of the filterFilter function in AngularJS without making

I have made changes to the AngularJS filterFilter function to allow for exact matches. This means that when searching for '1', only '1' will be returned, not '11', '12', and so on. function filterFilter() { return ...

Angular Screen Capture Creator

I am currently in the process of constructing a straightforward Angular screenshot generator utilizing this specific jQuery plugin. Here is the link to my project on Plunker This snippet showcases my index.html file: <!DOCTYPE html> <html ng-ap ...

Retrieving the name and value of inputs from an Angular controller

Here are the inputs I have and I need to extract both their 'name' and 'value': <script> $scope.userAttributes = response.userAttributes.split(','); //$scope /* OR */ self.userAttributes = response.userAttributes.spl ...

Can data be presented in AngularJS without the use of scope variables?

I have a method: <span ng-init="getJobApplicantsList(jobId)">(number should be display here)</span> Is there a way to display the data without having to store it in a scope variable? I need to use this method in many places. This is my cont ...

Issue with AngularJS $scope data binding not working properly inside an iframe element

I find myself in a foreign place.... in my home, why does this happen: Video Url: {{exercise.videos[0].url}} It works correctly by displaying the video url... but: <iframe src="http://fast.wistia.net/embed/iframe/{{exercise.videos[0].url}}"></ ...

The ashx file is triggered with every server control postback in jqgrid

I have an asp .net webforms page with a jqgrid, as well as several other server controls. The jqgrid is populated using an ashx file which retrieves data from the database and binds it successfully. Challenge Whenever a postback occurs (such as from a dro ...

Executing a function by clicking on a DIV with the value of a textbox, instead of clicking directly on the textbox

Here is a function I have: $("#border-radius").click(function(){ var value = $("#border-radius").attr("value"); $("div.editable").click(function (e) { e.stopPropagation(); showUser(value, '2', this.id) $(this).css( ...

AJAX parsing through JSON files generated by PHP

Need help with sending a json to an ajax call and reading it when it's sent. Plus, the json structure seems off due to the backslashes... This is the ajax function in question: function find(){ var type = $('#object_type').val( ...

Determine the current directory being called in Grunt, without confusing it with the directory of the gruntfile

If Grunt is installed in a folder called /foo, but my current directory is /foo/bar/baz, and I execute "grunt sometask" from within my current directory, how can I programmatically determine the path of the current directory where Grunt was called? In othe ...

Hiding javascript code within comment tags "<!-- //-->"

Years ago, I started a practice of enclosing all my JavaScript code in files with <!-- Code goes here //--> I'm not entirely sure why I did this. Maybe it was to hide the code from old browsers, is that right? Do I still need to do this? I ...

Receiving error messages about missing images in my React project

I am new to programming and I have encountered an issue while running my React project. When I use the command npm start, I noticed that some image resources are not being packaged properly, resulting in certain images disappearing when the website is run ...

Encountering a jQuery error while trying to utilize the $window.load

I have a code snippet that is functioning well when wrapped within a document ready event: jQuery(document).ready(function($) { $('tr[data-name="background_colour"] input.wp-color-picker').each(function() { //this section works fin ...

Sending a path containing '/' to either the $resource or $http factory

Is there a way to send a parameter containing / to the Factory? Trying to achieve something like this: .factory('MyData', ['$resource', function ($resource) { return $resource('http://1.2.3.4/:urlFragment', { urlFragmen ...

The WebDriver server at http://192.168.1.34:62334/wd/hub took too long to respond, causing a timeout error in Selenium

Currently, I am diving into the book "AngularJS: Novice to Ninja," and have completed the necessary installations by running the following commands: npm install karma npm install jasmine npm install karma-chome-launcher npm install protractor Following t ...

What is the reason behind the Typescript compiler not converting .ts files to .js files automatically?

Displayed below are the folders on the left showcasing my Typescript file in /src (blue) compiled into Javascript in /dist (purple) using tsc. https://i.stack.imgur.com/7XNkU.png In the source file on the left, there is a reference to a .ts module file t ...

Retrieve the part of a displayed element

Presently, I am developing a modal system using React. A button is located in the sidebar and the modal is represented as a div within the body. In the render function of the main component of my application, two components are being rendered: MyModal M ...