AngularJS code snippet for md-checkbox to act as a radio button

I want to make md-checkbox act like a radio button in Angular 1. I attempted the code below, but it does not seem to be functioning as expected. Is this even possible?

<md-checkbox class="md-primary" ng-model="color" name="color" ng-repeat="c in colors" ng-true-value="'{{c.id}}'" ng-false-value="">
    {{c.name}}
</md-checkbox>

Any help would be greatly appreciated.

Answer №1

Avoid using a checkbox when a radio button is required. To make a radio button look like it has been checked, simply apply the same classes used by md-checkbox to the radio buttons.

Using the incorrect form controls solely for visual appeal can lead to various accessibility issues, particularly on mobile devices or for users relying on screen readers. Instead of compromising usability, consider styling the radio button to resemble a check mark if necessary.

Answer №2

Using ng-checked and ng-click simplifies handling checkboxes without needing an ng-model for each one. Check out this demo for more details.

HTML:

<input type="checkbox" ng-repeat="item in items"
           ng-click="toggleItem(item)"
           ng-checked="selectedItem.id === item.id">
    {{item.name}}
</input>

CONTROLLER:

app.controller('MainCtrl', function($scope) {
    $scope.items = [{id: 1, name: 'Option A'}, {id: 2, name: 'Option B'}];
    $scope.selectedItem; //Initial selection can be set here

    $scope.toggleItem = function(item){
        $scope.selectedItem = item;
    };
});

Answer №3

It should be as shown below,

 <md-checkbox class="md-primary" ng-model="color" name="color" ng-repeat="c in colors" ng-true-value="'c.id'" ng-false-value="">
        {{c.name}}
 </md-checkbox>

DEMO

// Code example

var app = angular.module('app', ["ngMaterial"]);
app.controller('myCtrl', function($scope) {
  $scope.colors = [{
    id: '001',
    name: "pink"
  }, {
    id: '002',
    name: "yellow"
  }];


});
<!DOCTYPE html>
<html ng-app="app">

<head>
  <title>Testing angular md-select with material 0.11.0</title>
  <link rel="stylesheet" href="style.css" />
  <link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/angular_material/0.11.0/angular-material.min.css" />
  <script src="http://code.jquery.com/jquery-2.1.4.min.js"></script>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular.min.js"></script>
  <script src="https://ajax.googleapis.com/ajax/libs/angular_material/0.11.0/angular-material.min.js"></script>
  <script src="https://code.angularjs.org/1.4.1/angular-animate.js"></script>
  <script src="https://code.angularjs.org/1.4.1/angular-aria.js"></script>
</head>
<body ng-controller="myCtrl">
   <md-checkbox class="md-primary" ng-model="color" name="color" ng-repeat="c in colors" ng-true-value="'c.id'" ng-false-value="">
    {{c.name}}
  </md-checkbox>
</body>

</html>

Answer №4

If you include ng-true-value="{{c.id}}", it should function properly.

As stated in the official Angular Material documentation:

ng-true-value - expression - The designated value when the expression is selected.

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

After converting my HTML elements to JSX in Next.js, I am experiencing issues with my CSS files not being applied

Hey there, I'm currently working on a website using Next.js. I've converted the HTML elements of a page to JSX elements but for some reason, the CSS of the template isn't showing up. I've double-checked all the paths and even updated th ...

Select the text within the span element and paste it into the clipboard

Here is the html code I am working with: <label class="form-group" i18n>Send us your email:</label> <span (click)="select_email()" id="select_email"><a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="cb939282e5b9 ...

How can I make the expired date display in red color in a JavaScript date property?

In my HTML, I have 4 different dates. The expired date "END MAR 20" should be displayed in red color instead of green. The next date, "END APR 29," should be in green color. All preceding dates should also be displayed in red. I have successfully filtered ...

Achieving secure HTTPS connection with socket.io

At the moment, my setup involves: let connectTo = "http://myip:myport"; var socket = io.connect(connectTo, {secure: true}); on the client side and const port = myport; const io = require('socket.io')(port); on the server side. I am looking to ...

Creating a custom template in VueJS

How can I dynamically generate a specific template in the beforeCreate hook based on a given condition? I have three different roles in my application and I need to create a unique template for each role. ...

Loop through a nested array of objects within the same row using ng-repeat

Struggling with binding data from a nested array in the same row. Here is my Array of Objects: $scope.Record = [ { Name:'Adam', Months:[ {Month: 'Jan', Value: 10, cust:2}, {Month: 'Feb', Value: 30, cust:2} {Month: 'M ...

What is preventing me from changing the text color of this span element to white?

For some reason, I'm trying to make the first two texts of each line appear in white, but my CSS doesn't seem to affect the span elements generated by JavaScript. Take a look at the code snippet below for more details. I'm still learning ho ...

The use of fs.writeFileSync is invalid and will not work for this operation

Encountering an issue while working with fs in next.js, receiving the following error message: TypeError: fs.writeFileSync is not a function Here's a snippet from my package.json: resolve: { fallback: { "fs": false }, } ...

Send data between components using Angular directives

I have created a directive for a simple button App.directive('questionbutton', function(){ return { restrict: 'E', templateUrl: 'views/question-button.html' }; }); Another directive I have implemented ...

Running and animating two Three-JS objects simultaneously: A step-by-step guide

I have created two 3JS objects and scenes for educational purposes. Each one is stored in separate PHP files (not in the jsfiddle). However, I'm facing an issue where adding a second object causes the first object to stop animating. How can I troubles ...

Typescript error encountered while attempting to fetch repository from Redis OM

I'm currently working on developing a shopping cart system with the use of expressjs, typescript, and redis OM for the in-memory database. As I created a schema using Redis OM, I encountered an error The error message says: 'Property 'fetc ...

Issue with JavaScript focus not functioning properly

Why is the Javascript focus not working in the following code snippet? HTML <div class="col-md-6" style="border:none; margin-top:2px; padding-right:5px"> <input type="text" id="edtunt" ng-model="Unit" class="form-control" placeholder="Edit ...

JSON script and datetime formatting procedures

Here is my PHP/JSON script from localhost: If you need to download the PHP files to edit them in your answers, you can find them here. The link to the JSON file is mentioned in large-schedule.js. Instructions on usage are provided. The script partially w ...

What is the reason behind being able to use any prop name in a React function without explicitly mentioning it?

Currently, I'm delving into the world of mobX-state-tree, and in the tutorial code that I'm exploring, there is an interesting piece that caught my eye. const App = observer(props => ( <div> <button onClick={e => props.store. ...

ng-repeat inside a directive fails to update upon adding or modifying objects in the current collection

Currently, I am facing a challenge with populating a treeview that contains objects with nested arrays. If you want to see a live example of my code and the issue I'm dealing with, check out this working plunker. Let me break down how the code is st ...

Creating a modal dialog with AngularJS directive

My goal is to transfer a value from a button's data-id attribute to a modal dialog. This is what I have implemented: Button HTML <button ui-sref=".modal" btn-attr-click data-id="{{data.nip}}">Simulate</button> Angular State : var rout ...

Node.js Express refuses to serve .js files with absolute URLs

I have encountered a perplexing issue with two files located in /public/widget, namely help.html and help.js http://localhost:8084/widget/help.html When entered into the address bar, it functions normally However, when attempting to access http://local ...

Obtaining Post Parameter Data in a Vue File Using Nuxt.JS

Currently in the process of incorporating the Paytm payment gateway into my Nuxt JS project. However, I am encountering difficulties in retrieving the post data that Paytm is using to redirect to my callback URL. ...

Leveraging JavaScript to enable automatic identification of buttons on a webpage

My website features specific buttons placed within articles and also in the head/body/footer sections. I have a plan to make it so that when a user clicks on a button, JavaScript code will determine if the button is inside an article or outside of it, then ...

The mystery behind React rendering twice, even when React strict mode is disabled

My code is error-free, but React seems to render this component twice for some reason. I can't figure out why. Here is an image showcasing the issue: https://i.stack.imgur.com/hKvug.png Index.js Page : import LandingPage from '../components/Ho ...