Toggling the boolean value of an object in AngularJS by clicking a button

I am currently working on a ToDo application as a way to familiarize myself with Angular JS.

Within my application, there is an array named toDoList, where I store objects based on user inputs received from a modal window.

These items are then displayed on the user interface.

One of the properties in these objects is done, which is initially set to false.

My goal is to have a button that allows users to mark a task as completed. When this button is clicked, I want the value of done for the specific object in the toDoList array to toggle between true and false. It should only affect the individual object within the generated ng-repeat element, not all objects in the list.

The button responsible for this action is labeled as itemComplete.

I attempted to use a booleanVal toggle within the ng-click, but encountered errors in the console.

Index.html

<div class="row newItem" ng-show="toDoList.length > 0"
     ng-repeat="item in toDoList">
    <div class="col-2">
        <button class="itemComplete btn"
                ng-click="item.done.booleanVal = !item.done.booleanVal">
          <i class="far fa-check-circle fa-2x"></i>
        </button>
    </div>
    <div class="col-8">
        <h4>{{item.project}}</h4>
        <p>{{item.task}}.</p>
    </div>
    <div class="col-2">
        <button class="btn deleteItem"
                ng-click="deleteItem($index); getTaskCount()">
          <i class="far fa-times-circle fa-2x"></i>
        </button>
    </div>
</div>

App.module.js

$scope.toDoList = [];

$scope.addNewToDoTask = function () {
    $scope.toDoList.push({
        project: $scope.projectInput,
        task: $scope.taskInput,
        done: false
    });
    $scope.projectInput = "";
    $scope.taskInput = "";
};

Answer №1

Consider substituting item.done for item.done.booleanVal in the ng-click statement.

Answer №2

An Issue with Boolean Values:

Encountered Error: TypeError: Cannot create property 'booleanVal' on boolean 'false'

at fn (eval at compile (VM335 angular.js:16421), <anonymous>:4:461)
at callback (VM335 angular.js:28954)
at ChildScope.$eval (VM335 angular.js:19396)
at ChildScope.$apply (VM335 angular.js:19495)
at HTMLButtonElement.<anonymous> (VM335 angular.js:28958)
at defaultHandlerWrapper (VM335 angular.js:3826)
at HTMLButtonElement.eventHandler (VM335 angular.js:3814)

This seems quite apparent:

<button class="itemComplete btn"
        ̶n̶g̶-̶c̶l̶i̶c̶k̶=̶"̶i̶t̶e̶m̶.̶d̶o̶n̶e̶.̶b̶o̶o̶l̶e̶a̶n̶V̶a̶l̶ ̶=̶ ̶!̶i̶t̶e̶m̶.̶d̶o̶n̶e̶.̶b̶o̶o̶l̶e̶a̶n̶V̶a̶l̶"̶>̶
        ng-click="item.done = !item.done">
  <i class="far fa-check-circle fa-2x"></i>
</button>

It's important to note that boolean values are primitives and do not support properties.

View the DEMO on PLNKR

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

Converting an Array of Users to an Array of Email Addresses Using Google Apps Script

I am currently in the process of creating a script using Google Apps Script. My goal is to verify whether a user with the email address [email protected] has viewing or editing privileges for a folder. If the user does not have either privilege, I wa ...

How can we retrieve the key from a value and then multiply the price by the quantity?

Whenever I choose a category, the price is displayed in the select box. However, when I input a value in the quantity box, it multiplies the price and quantity to show the total. But instead of fetching the ID of the price and quantity multiplied from the ...

Issues encountered with sending post requests to a yii2 application when using Angular4

After implementing the following code: this.http.post('http://l.example/angular/create/', {name: 'test'}).subscribe( (response) => console.log(response), (error) => console.log(error) ); I encountered an error on ...

Is there a way to configure MaterialUI XGrid filters to target and filter by the renderCell parameters instead of the backend data source?

While utilizing MaterialUI XGrid to showcase rows of information, I am facing an issue with filtering. Currently, filtering can only be done based on the backend row values rather than what is displayed in the cell. For instance, consider a column named U ...

Verify whether a string includes any of the elements within an array

Imagine I have a string: const subject = "This process is flawless" and an array: const matchArray = ["process","procedure","job"] If subject contains any keyword from matchArray, I want to do the following: if (su ...

In Laravel Blade, I am looking to display a Modal popup and dynamically pass data based on the user's ID

When a user clicks on the "View Details" Button, I need to display a popup modal with information specific to that user. The data for each user is stored in the $user variable. I would like to achieve the same functionality as demonstrated on this website ...

The Response.Write function is not functioning properly in the production environment

I have created a C# WebService (ASMX) with the code snippet below: if (!SomeValidation()) { //context.Response.ContentType = "application/json"; //context.Response.ContentType = "text/plain"; context.Response.ContentType = "application/text"; ...

Adjusting the z-axis rotation in a three.js animated scene

Is there a way to add a function that changes the Z rotation value of the teapot from within the updateTeapot function? I came across this helpful answer on Three.js camera tilt up or down and keep horizon level. However, I am unsure how to integrate a z ...

Transforming an Array into a String and Extracting a Substring

I started with a numeric array in JavaScript and converted it to a string using the join method. However, when attempting to extract a substring using substr, I encountered a technical issue. Can someone please assist me with this problem? var array = [85 ...

What causes the discrepancy in calculating marginTop on a desktop browser compared to a mobile browser?

In the top screenshot, you can see a representation of my Pixel 6XL connected to my laptop in USB debug mode. The checkered area represents the URL bar on the Chrome browser displayed on my Pixel device. Below that, the second screenshot shows the view fr ...

What is the reason for the svg animateTransform only being triggered on the initial item?

When hovering over the first item, the animations for the others would also trigger. However, when hovering over the second item, the other items wouldn't work. How can I ensure that each item's animation triggers one by one? body { displa ...

Shiny app requires double-click for Highcharter plot updates in R

Here is my code snippet that relates to the question I asked earlier today. However, I am facing a new issue that has me stumped. After clicking the actionButton to update the chart, it seems like the chart only refreshes after the second click. Surprisi ...

Deactivate a chosen anchor button when clicked within a loop of elements

In my laravel project, I have a foreachloop that is rendering multiple buttons. I want to disable or remove only the button that is clicked, so I tried the following code: @foreach() <a onclick="remvebtn()" class="btn">My ...

Updating the default date format input fields for React-bootstrap-daterangepicker in the United States

Hey there, I'm a newcomer to the world of React and Javascript and I've encountered an issue that has me stumped. I'm trying to change the default US dateformat in my React-bootstrap-daterangepicker, but I'm struggling to figure out how ...

Getting the value of a JavaScript variable and storing it in a Python variable within a Python-CGI script

Is there a way to capture the value of a JavaScript variable and store it in a Python variable? I have a Python-CGI script that generates a selection box where the user can choose an option from a list. I want to then take this selected value and save it ...

node.js: The Yahoo weather jQuery plugin fails to display any data

After successfully implementing node.js with jQuery and the plugin from , I now aim to utilize the weather data for a different purpose rather than directly inserting it into the HTML. However, I am encountering difficulties in accessing or displaying the ...

Unable to sign up for WordPress function

I'm having trouble getting my function registered properly in WordPress, no matter how many times I try. So far, here's what I've done: Inserted code into themes functions.php function test_my_script() { wp_register_script( 'custom-s ...

What reasons underlie the existence of various methods for importing Modules in JavaScript?

I'm confused about the distinctions when it comes to importing a JavaScript module in various ways such as: CommonJS ES5 ES6 NodeJS Typescript What is the reason for having multiple methods of importing JavaScript modules? Is the concept of a "modu ...

Determine the quantity of items within a JSON array

There is a json array located at the following url: http://localhost/heart/api/restApiController/dataset.json The structure of the json array is as follows: [ { Weight: "3", Smoking: "1", Exercising: "0", Food_habits: ...

Creating numerous objects in Node.js using Mongoose populate

My current task involves creating an array of dishes that a logged-in user has selected as their favorite. However, I am facing an issue where instead of having one array of objectIDs for the dishes (e.g. dishes:[123456,5678910]), I am getting two separate ...