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

Issue with SVG on tainted canvas causes IE security error when using toDataURL

In my Angular JS directive, I have implemented a feature to export SVGs to PNG. While this functionality works seamlessly in most browsers, it encounters a security error in IE. Despite my numerous attempts to troubleshoot the issue, I have been unable to ...

Issue with directive not activating when attribute is changed

I am facing an issue with my website where users can make selections from two dropdowns, and based on those values, attributes are sent to directives for a corresponding function to be called. The problem I'm encountering is that the directives are n ...

"Encountered a runtime error while trying to execute the doubleClick() function using Pro

Encountering the following issue: "WebDriverError: Unable to convert: Error 404: Not found" while running a test with protractor: browser.actions().doubleClick(elem).perform(); or browser.actions().click(elem).click(elem).perform(); Uncertain of t ...

Guide on exporting Excel data using Angular and TypeScript

My project involves managing a table of information that includes fields for FirstName, LastName, PhoneNumber, Age, and Date. I've created a function that allows me to export the data to an Excel file, but I only want to export specific fields like Fi ...

Angular - delay execution until the variable has a value

When the ngOnInit() function is called, the first line of code retrieves a value from local storage which is then used to filter data from the database. Both actions happen simultaneously, resulting in an issue where I don't receive the expected resu ...

Scrolling horizontally in ng-grid version 2.0.11

In my application, I am utilizing a ng-grid with auto height and width. The challenge arises when dynamically adding columns to the grid. If there are too many columns to display, instead of showing a horizontal scrollbar, the columns shrink in size and ap ...

The error encountered is: "Unable to modify the 'x' property as it is readonly for the '[object Array]' object."

I've attempted various methods to modify this problem, regardless of how it's explained on different Stack Overflow threads. I am faced with an obstacle where I have an array composed of objects, and my goal is to iterate through the array and mo ...

Utilize key-value pairs to reference variables when importing as a namespace

Is it feasible to utilize a string for performing a lookup on an imported namespace, or am I approaching this the wrong way? Consider a file named my_file.ts with contents similar to: export const MyThing: CustomType = { propertyOne: "name", ...

The request to upload an image to Cloudinary using AngularJS is being blocked due to the origin http://localhost.com:8000 not being allowed by Access-Control-Allow-Origin

I've been attempting to upload images to Cloudinary through my Angular controller without using any libraries. The POST requests are unsigned, and interestingly, I was successful when I tested it with jQuery on a static HTML page. Here is the code sn ...

Ng-repeat failing to apply checkbox functionality

When someone clicks on the levels in a list, I need to call a method in AngularJs. However, clicking on the checkbox within the ng-repeat does not trigger any action. Strangely, when I click on the checkbox outside of the ng-repeat, it works fine. Here is ...

AngularJS failing to execute Ajax request

I've been working on implementing an Ajax call in a controller to retrieve a list of all the students, but I'm having trouble getting it to work when clicking a button. Here is the code that I've written - can anyone help? module.controll ...

Execute the unknown function parameter

Trying to figure out how to convert an argument into an anonymous function, but struggling to find clear instructions. I know how to cast on variable assignment, but unsure if it's possible and how. Working with lodash where the typings specify the a ...

AngularJS model linking two input fields

Here is an example of an object I am working with: MyObj{ Groups:[{name:"test1": xdata:"[1,2,3]" }, {name:"test2": xdata:"[5,6,7]" }] } I need to display these values in two different views on the same page. The first view will render xData and ...

The functionality of Angular services is not available for use in Jasmine tests

As someone who is relatively new to Jasmine Testing and the Angular framework, I find myself in a unique situation. I am currently struggling with referencing my service functions in my Jasmine tests. Here is a snippet of my Angular Service Initialization ...

How can you define types for abstract React components in Typescript?

Currently facing a challenge when it comes to typing an abstract component in Typescript, especially after having extensive experience with Flow. The code snippets below are based on Typescript 3.8.3. Here is the relevant code: const useSlot = (): [React ...

Checking the parameters passed to a function in Typescript: A step-by-step guide

Currently, I am working with Typescript and then transpiling my TS code into JavaScript. However, I have encountered an issue that I am struggling to resolve. The error message I am facing is as follows: Error Found in TypeScript on Line:2 - error TS230 ...

What is the process for integrating ng-bootstrap into an Angular 5 project?

Has anyone encountered issues loading ng-bootstrap in their Angular project? I'm experiencing difficulties and would appreciate any insights. Thank you for your help! This is my app.module.ts file: import { BrowserModule } from '@angular/platf ...

What is the method for extracting children from a singular object in json-server rather than an array?

I am currently utilizing json-server as a mock-backend to fetch child data from a single object. The main table is called sentinel and the secondary table is named sensor It can be observed that sensors is an array, while sentinel is an object. I have ...

Creating a nested array of objects in AngularJS that includes an array of objects

Struggling to sort through a nested array of objects using my own objects, based on idSubject. Unfortunately, the results aren't quite what I expected. I have articles that are associated with subjects, as well as an array of objects (representing th ...

Guide on exporting a submodule within a TypeScript package

My aspiration is to develop a Typescript library that emulates the structure of popular libraries like RxJS and Angular Material, which are divided into submodules. RxJS and Angular exhibit a way to import features using syntax like this: // RxJS import ...