Tips for making a Toggle button in Angular2

I'm exploring how to utilize a Radio button as a toggle in Angular2.

My goal is to capture the value of the Radio button when it changes without the need for creating a form.

Any assistance would be greatly appreciated.

[edit]:

This is what I tried:

<div class="form-group">
                <label for="gender">Gender</label><br>
                  <input type="radio" name="gender" value="male" checked> Male&nbsp;
                  <input type="radio" name="gender" value="female"> Female<br>
              </div>

I added this code within a form and was able to retrieve the gender value through form details. However, I want to achieve this without using a form.

Answer №1

Implement this code snippet in your component:

public gender = [
        'male',
        'female'
    ];
onGenderChange(e) {
        console.log(e);
    }

Add the following code to your HTML template:

<div *ngFor="let value of gender">
    <input (change)="onGenderChange($event.target.value)" type="radio" name="gender" value="{{ value }}">{{ value }}
</div>

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

Enhance the structure of this array data model

Currently in the process of creating a small script to tag coordinates in an array: var countries = ['spain','australia']; var leftX = ['270','575']; var topY = ['60','220']; When I need to ...

Creating a universal alert function in AngularJS is a useful tool that can improve the functionality

I have been exploring AngularJS and I am looking to create a universal alert function that can be used across all controllers for validation and other purposes. Here is my service: JodoModule.service("CommonSrv", function ($rootScope) { this.showAler ...

Styling for Ajax Forms when traditional methods don't work

I'm working on an Ajax form and I want to ensure there's a solid fallback in case the user has JavaScript disabled. Currently, the form functions without JavaScript, but the user sees the JSON string displayed as plain text without any styling du ...

Jquery causing issues when used alongside another library

I am utilizing multiple jQuery libraries for a website with 2 plugins, but they are conflicting. I do not possess extensive knowledge of jQuery, so a beginner's guide would be greatly appreciated. The "jQuery-loader" is causing issues with the "jQuer ...

Display JSON values in sequence using Material-UI animations

I have received an array of JSON data from the API that looks like this: "fruits": [ { "id": "1", "fruit": "APPLE", }, { "id": "2", "fruit": ...

What could be causing json_encode to return null values for form inputs?

My first attempts at submitting an AJAX form are causing some issues. I have a basic HTML form, a little JS, and a php validation file. During testing, I noticed that some of the form inputs are returning as "null" in my console.log, while others are not. ...

The height of the browser action will not return to its original state

I'm currently working on an extension that provides responses based on user text input. However, I'm running into an issue where the height of the browser action won't reset properly. I've tried various methods to fix this problem, in ...

Incorporate additional libraries like jQuery into your Vue.js application

I am facing an issue with connecting a third-party js library to a vue.js component main.js ... require('jquery') import Vue from 'vue' import VueMdl from 'vue-mdl' import App from './App' import Auth from './ ...

Tips for transferring the id from a delete button to a delete button in a popup dialog box

In my frontend application, there is a table where each row corresponds to an item. For every row, there is a "Remove" button that triggers a warning popup upon being clicked. The intention is to pass the item's ID in this popup so that if the user co ...

Command to conceal components for users visiting as guests

I'm looking to develop a directive that hides specific elements for guest users. Here is the current code I have: angular.module('someMod') .directive('premiumUser', premiumUser) .controller('PremiumUserCtrl', Pr ...

Setting a loader for when a component renders for the first time in Nuxt 3

In my nuxt 3 project, I am utilizing the element plus library to create a page with 3 tabs. Each tab's content is divided into its own component, and I have set conditions to initially render only the first tab component. However, when switching to th ...

Is the AngularJS `$http` cache capable of storing server errors in its cache?

When working with Angular, using $http.get(url, {cache:true}) will cache server responses for easy access. But what happens when the GET request to the server fails? This could be due to a temporary connection issue or a server response such as a timeout. ...

Exploring the practical applications of Jade template along with AngularJS

I'm currently working on a website project with Node.js and Express. I need help making the divisions on the page dynamic. Is Jade the best tool for this, or is there another method I should use? Also, what exactly is AngularJS used for? I've tri ...

Adding a directive with an ng-click dynamically

UPDATE: view the demo on Plunker: https://embed.plnkr.co/fHPQfbUQ1lvZBJsDNQR5/ An issue arises when I try to include my directive in my HTML code, triggering an error: "http://errors.angularjs.org/1.5.6/$parse/syntax?p0=%7B&p1=invalid%20key&p2=17 ...

Utilizing Next.js to dynamically update data within a div element upon

I have a table set up to display data and I want to transfer the row data into a div when the user clicks on a table row: Currently, I can successfully get the data onclick: const handleClick = (rowData) => { console.log(rowData); } However, I am ...

Displaying JSON data in a popup window resembling a download prompt

I'm a beginner in front end development and facing difficulty in displaying JSON in a new window. Currently, I'm allowing users to download the JSON file like this var blob = new Blob([$scope.data], {type: 'json'}); ...

What is the impact on performance when multiple observers are monitoring the same value?

Summary: When working with AngularJs, does using the same variable for data-binding in multiple places result in the variable being dirty-checked multiple times during each $digest-cycle? Extended inquiry: I need to display a variable from the scope in ...

Unable to retrieve props from server-side page to client-side component in a Next.js application router

Currently, I am utilizing app router alongside Next.js version 13.5. Within my /dashboard page (which is a server component), there is an ApiKeyOptions client component embedded. However, when attempting to pass props from the dashboard page to the ApiKeyO ...

Ways to fetch additional Product Cards using JSON and a nextPage parameter

I'm facing difficulties in nesting a fetch inside another fetch. I'm not sure if this is the correct approach, but my goal is to utilize Vanilla JavaScript to fetch another JSON from the nextPage URL within the JSON list when the "load more" butt ...

What could be causing the absence of console.log output in my Netlify function logs?

I am facing an issue with my Netlify function that is scheduled to run every minute using the @netlify/functions package. The function makes API calls and logs the response, but I cannot see any output from the console.log statement in the Netlify function ...