Exploring Angular 2: How to Retrieve the Value of a Radio Button

How can I retrieve the value of the radio button that is clicked in app.component.html from within app.component.ts?

app.component.html

<div class="container">
<div class="row">
    <div class="col-sm-3 well" style="width: 20%">
        <h4>Narrow Your Results</h4>
        <div class="well">
            <h5>Rating</h5>
            <form [formGroup]="ratingForm">
                <input type="radio" formControlName="rating" value=100
                    (click)="filterByRating(ratingForm.value)"> All Grills
            </form>
        </div>
    </div>
</div>

app.component.ts

@Component({
    selector: 'app-rootl',
    moduleId: module.id,
    templateUrl: app.component.html'
})
export class AppComponent implements OnInit {

    ratingForm = new FormGroup({
        rating: new FormControl(),
    });


    filterByRating(rating: number) {
        console.log("rating = " + rating.valueOf());
    }
}

When calling filterByRating(), instead of getting "rating=100" printed out, I see "rating = [object Object]". How can I access the actual value of the object?

Answer №1

When triggering the (click) event, ensure you are only passing the radiobutton control to the filterByRating() function instead of the entire form. Here's an example:


<input type="radio" formControlName="rating" value=100 (click)="filterByRating(ratingForm.controls['rating'].value)">

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

Props used in styled components are effective, although they may trigger a warning message stating, "Warning: Received `true` for a non-boolean attribute `cen`."

Caution: A non-boolean attribute "cen" received a value of "true". If you intend to render it in the DOM, provide a string instead: cen="true" or cen={value.toString()}. While using Props in Styled-Component with TypeScript and Material-UI, everything func ...

How to pass parameters to an AngularJS controller without using the question mark syntax

Is it possible to send data to a controller without using a question mark in the URL? For instance: Instead of doing this: $location.path("/inventory/product").search({ id: 1 }); The resulting URL appears as localhost:16684/#/i ...

Encountering a console error in a TypeScript Express app when using MUI and Preact: "Unexpected prop `children` passed to `InnerThemeProvider`, was expecting a ReactNode."

I'm working on integrating MUI with a Preact app. In VSCode, everything seems to be set up correctly, but when I try to view it in the browser, nothing renders and I get this console error: react-jsx-runtime.development.js:87 Warning: Failed prop type ...

Using AngularJS to filter and order items in an ng-repeat loop with multiple variables

I hold a dataset with employment history. There are two fields for the employment dateTo: yearTo, monthTo To accurately sort the employment history by the most recent dateTo, I utilize the following code: <tr id="employment_history_" ng-repeat="histo ...

Is a promise already included in Angular 1.4's $http for HTTP GET requests, or do I need to create my own?

My mind is getting all tangled up the more I delve into online materials about $q and $http. If I make a $http.get call, does that automatically involve a promise? Should I also use $q in this scenario? ...

"Real-time communication: Using SignalR to send personalized messages to the logged in

I have a web app project and an Angular 2 project. I want to use SignalR to send messages from the server. I came across this article that explains how to implement it. However, I am unsure of how to send a message to the current user. This is the C# code ...

Extract the JSESSIONID and generate a new Cookie using AngularJS

Currently, I am utilizing a Web RESTful API from my client within AngularJS. app.controller('LoginController', [ '$http', '$cookies', function($http, $cookies) { this.credentials = {}; this.http = $ ...

Unwrapping the potential of $http promise in AngularJS

Starting with AngularJS, I am curious about how to handle the response of "response.data" as a typical function. The issue arises because when using $http, it generates a promise, causing the function to finish execution before returning the server respons ...

Deploying AngularJS and Ruby on Rails applications on Heroku platform

I have successfully completed an application using Ruby on Rails and AngularJS. It is fully functional and operational on my local machine. However, I am facing difficulties when trying to deploy it to Heroku as I keep encountering the following error: re ...

Utilizing AngularJS: Using ng-click with ng-repeat to retrieve all data simultaneously

Having a simple ng-repeat: <div ng-repeat="x in names"> <h4>{{x.productid}}</h4> <h4>{{x.newquantity}}</h4> <h4>{{x.total}}</h4> <button ng-click="addInfoAboutOrder(x)">Add Info</button> ...

Is ngOnDestroy executed within Angular services?

Is there a way to confirm if the ngOnDestroy method in my UserServiceService class is functioning properly? I want this service to continue running until the project starts and ends, with ngondestroy method executing once the application (website) is clo ...

Developing web components using angular.js while ensuring compatibility with IE11

I have an angular.js application where I need to initialize a web component. Everything runs smoothly on different browsers, however IE11 seems to have problems with document.importNode The angular.js onInit function is as follows: vm.$onIni ...

Merging AngularJS and CodeIgniter for efficient data transferIn this article, we explore the

Recently embarking on my journey to learn AngularJS, I've been considering building an application utilizing Codeigniter as the backend (as an API to handle data operations with a MySQL database) and AngularJS as the frontend framework. My main query ...

Is your $http request causing an XML parsing issue?

I'm attempting to utilize the $HTTP method from angularJS to access my restful web service. On entering my web service URL in the browser, I receive a result of APPLICATION/JSON type. {"id":20418,"content":"Hello, World!"} The URL for my web servic ...

What steps can be taken to address the InvalidPipeArgument error when working with dates?

When attempting to format a date in a specific way using the pipe date, I encountered an error: Uncaught Error: InvalidPipeArgument: 'Unable to convert "25/01/2019" into a date' for pipe 'e' at Xe (main.fc4242d58c261cf678ad.js:1) ...

Different methods to prompt TypeScript to deduce the type

Consider the following code snippet: function Foo(num: number) { switch (num) { case 0: return { type: "Quz", str: 'string', } as const; case 1: return { type: "Bar", 1: 'value' } as const; default: thr ...

Guide on how to show the index value of an array on the console in Angular 2

Is there a way to show the array index value in the console window upon clicking the button inside the carousel component? The console seems to be displaying the index value twice and then redirecting back to the first array index value. Can we make it so ...

MXGraph has an issue where edges fail to be redrawn after being moved

Perhaps this question may seem trivial, but I am facing an issue in my code and seeking guidance from the community. I am working on a javascript mxGraph application within Angular7. Specifically, I have modified the ports.html example for my project. Wh ...

Setting an expiry date for Firestore documents

Is it feasible to set a future date and time in a firestore document and trigger a function when that deadline is reached? Let's say, today I create a document and specify a date for the published field to be set to false fifteen days later. Can this ...

The function 'makeDecorator' does not support function calls when being accessed

Resolved by @alexzuza. Check out his solution below - major props! The issue was with the node_modules folder in the ng2-opd-popup directory, it needed to be removed and the src/tsconfig.app.json file had to be adjusted accordingly. Make sure to also refer ...