What is the recommended approach for unchecking the last element in ngRepeat Check-Boxes with AngularJS?

I have implemented the Check-box model with AngularJS, following the guidelines provided at . Now I need to make sure that for certain sets of checkboxes, the last checkbox should uncheck all others when clicked. Can someone guide me on how to add an ngClick attribute to this last checkbox element using the check-box-model module?

Answer №1

Check out the solution below:

View Working Demo

<label ng-repeat="role in roles">
    <input ng-click="$last&&mytest()" type="checkbox" checklist-model="user.roles" checklist-value="role.id"> {{role.text}}
  </label>

If you click on the last checkbox labeled "admin", it will trigger an alert box.

Answer №2

I may not have extensive knowledge about checklist-model, but one approach you can take is by incorporating ngClick to the final element in this manner:

<label ng-click={{$last && yourFunction || ''}} ng-repeat="role in roles">
  <input type="checkbox" checklist-model="user.roles" checklist-value="role"> {{role}}
</label>

Although it will attach the ng-click attribute to all elements, it could potentially serve as a viable solution for your needs. This offers an alternative method to consider.

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

Issue with Telerik JavaScript when navigating away from page

While working on my Asp.net web application, I encountered a JavaScript error when trying to unload a page: An issue arose with setting the value of the property '_events': Object is null or undefined This problem occurred after merging/combi ...

Choose the default option from the ng-options

Here is the code snippet and information at hand: $scope.options = [ { id: 1, label: "My label" }, { id: 2, label: "My label 2" } ]; $scope.selected = 2; <select ng-options="opt.label for opt in options" ng-model="selected"> <option value= ...

Getting a specific value from a REST API array in JavaScript: Tips and tricks

After being stuck on this problem for 8 hours, I finally decided to seek help: In my JavaScript file, I am attempting to extract data from a REST API response. The response consists of an array of objects structured like this: [{"start":"2017-04-21 14:40 ...

I find it strange how the text automatically becomes highlighted every time I add attributes in React

Recently, I encountered a strange issue while working on my code. The text inside an attribute I was typing suddenly started highlighting without any reason. This not only disrupted my workflow by preventing shortcuts but also became really annoying, esp ...

Is there a way to limit the width of the input box once it reaches a specific pixel measurement?

One issue I'm facing involves the following code snippet: <html> <head> <script type="text/javascript"> function Expand(obj){ if (!obj.savesize) obj.savesize=obj.size; obj.size=Math.max(obj.savesize,obj.v ...

"Unveiling the Mystery: Retrieving the selected option at index x in AngularJS Jasmine

Exploring the Angular website, I came across an interesting unit test example which can be found here. Please note that you need to click on the protractor.js button as there is no direct link to the unit test code. Below is the snippet in question: elem ...

Angular 2 iframe balked at being shown

Whenever I enter a URL into a text input and press enter, my objective is to have the URL open in an iframe. However, some websites produce an error when trying to do so. Is there a method to successfully display a web page in an iframe? Error message: Th ...

Tkinter: unresponsive check box selection

I am currently working on a project that involves creating a series of checkboxes using a loop. I've made some modifications to existing code that should function in the same way I intend it to. However, I'm encountering an issue where the state ...

Session availability extends to subdomains, even though it may not be visible in a physical

Currently in the process of building a website Red Sec using a single account for all subdomains: Latest Updates Community Forum Personal Blog News Feed Support Us All pages share the same layout with different content by linking them to . Below i ...

What is the method for configuring automatic text color in CKEditor?

https://i.sstatic.net/yEM3p.png Is there a way to change the default text color of CKEditor from #333333 to #000000? I have attempted to modify the contents.css in the plugin folder: body { /* Font */ font-family: sans-serif, Arial, Verdana, "Tr ...

Unique form validation directive tailored to your specific needs

My current challenge involves validating a number based on its minimum and maximum values using angularJS. Initially, I attempted to utilize the built-in max validation provided by AngularJS for input number validation. However, it only seemed to work with ...

The DOM seems to be producing NaN when receiving user input

Currently, I am in the process of developing a depreciation calculator that utilizes user input fields to generate an alert with the resulting value. When I set the variables to predefined test values without incorporating user input, the calculator funct ...

Overlapping of JavaScript Array.push() function within nested arrays causing duplication

Recently, I encountered a puzzling situation while attempting to create arrays with three levels of depth. It may seem crazy, but this approach was the most suitable solution to my problem at the time. The code snippet below illustrates how these nested ar ...

Showing subcategories when clicked using only JavaScript

The solution can be found below I have created a dropdown menu that currently shows the submenu on hover using the following CSS: .main-menu ul li:hover > ul { display: block; } However, my design requires the submenu to appear on click and stay visi ...

An issue arose upon restarting my Eclipse software

Upon restarting my Eclipse, I encountered the following error message: "Plug-in com.android.ide.eclipse.adt was unable to load class com.android.ide.eclipse.adt.internal.editors.common.CommonXmlEditor." As a beginner, could you advise me on how to addres ...

An issue arises in React TypeScript where a callback function is encountering undefined values when using setState, but surprisingly logs the

Struggling with a React application I'm building, specifically with an issue that's got me stumped. Here's a snippet of code that's causing trouble: onFirstNameChange(event: any){ console.log(event.target.value) // this.setState ...

What sets apart local and global variables when it comes to JavaScript?

It has come to my attention that in the realm of JavaScript, there are instances where a variable is created within a function without explicitly declaring it with var. For instance: function myfunction() { x = 10; var y = 10; } What sets these ...

Troubleshooting port deployment challenges on GCP for a Node.js project

My current Node.js project has one HTTP server running on port 8080, with a service URL generated for it. However, inside the http.createServer function, I have set up another server for an NLP engine that is listening on port 8081. While I am able to ac ...

Executing a function in Angular without any delay using a timeout

I am having an issue with the progress call in the code snippet below - it is being triggered continuously without the 10-second delay. Can someone help me identify what I'm doing wrong? ctrl.progress = function (fileName){ if(ctrl.status < 1 ...

When using PHP to echo buttons, only the value of the last echoed button will be returned in JavaScript

I am encountering an issue when trying to define a dynamic var for button value in my JavaScript code. Despite it working in PHP, the same code does not seem to function properly in JavaScript. Imagine I have three buttons echoed using PHP with values 1, ...