Avoiding errors during radio value changes when loading a component in Angular

I need assistance with setting a date using radio buttons. I encountered an error when I tried to change the value while the component was loading.

html.component.html

...
<div>
    <input type="radio" name="period" [checked]="asPeroidBtn === '1W'" (click)="setDate( '1W' )"/><span>1 Week</span>
    <input type="radio" name="period" [checked]="asPeroidBtn === '1M'" (click)="setDate( '1M' )"/><span>1 Month</span>
    <input type="radio" name="period" [checked]="asPeroidBtn === '3M'" (click)="setDate( '3M' )"/><span>3 Months</span>
    <input type="radio" name="period" [checked]="asPeroidBtn === '6M'" (click)="setDate( '6M' )"/><span>6 Months</span>
</div>
...

html.component.ts

...
asPeroidBtn = '1W';
...
ngAfterViewInit() {
    this.setDate('1M');
}

setDate(period){
    this.asPeroidBtn = period;
}
...

Upon implementation, I encountered the following error:

ERROR Error: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value for 'checked': 'true'. Current value: 'false'.
    at throwErrorIfNoChangesMode (core.js:5625)
    at bindingUpdated (core.js:13962)
    ...

I would appreciate guidance on resolving this issue.

Answer №1

Replace ngAfterViewInit with ngAfterContentInit to avoid the error from happening.

To learn more, you can refer to this detailed explanation:

Understanding ExpressionChangedAfterItHasBeenCheckedError

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

execute a function for every regex match found within a string

When working with WordPress or PHP in general, I came across this interesting recommendation for email obfuscation, and I would like to: Convert email addresses to mailto links Encode the mailto links using str_13() Decode them client-side using JavaScri ...

How to update MongoDB documents with referenced objects using Mongoose?

Apologies for any language barriers. I am using node.js + express.js + mongoose.js Here is my schema in mongoose for groups: var groupSchema = new mongoose.Schema({ name: String, users: [{type: mongoose.Schema.ObjectId, ref: 'User'}] ...

Is there a way to arrange elements in an array based on the second value in each nested array?

I need help organizing an object {a: 1, b: 20, c: 3, d: 4, e: 1, f: 4} into the following format {a: 1, e: 1, c: 3, d: 4, f: 4, b:20} (sorting the object numerically in ascending order) using a specific set of steps. Here are the steps I have outlined: ...

Issue with CasperJS: The function this.waitForUrl is not defined and is causing an error

My casperJS script handles form filling, however I encountered the following error message: A TypeError occurred: 'undefined' is not a function (evaluating 'this.waitForUrl') I suspect this might be an issue with using an outdated ver ...

Failed commitments in Protractor/WebDriverJS

WebdriverIO and Protractor are built on the concept of promises: Both WebdriverIO (and as a result, Protractor) APIs operate asynchronously. All functions return promises. WebdriverIO maintains a queue of pending promises known as the control flow to ...

Removing undesired entries from a table view using AngularJs

In my table, there is a column called status which could have values like 'Open', 'Closed', 'Verified' and 'Rejected'. I am looking for a way to implement a filter in ng-repeat that will hide the rows with the statu ...

Switching focus between windows while working with React

My current setup involves using React. Every time I run yarn start, I can begin coding and see the changes in my browser. However, there's a recurring issue where my terminal keeps notifying me about errors in my code every 10 seconds or so. This cons ...

"The reactivity of VueJs array of objects is not triggering properly when there is a change in

My state includes an array of objects structured like this: data() { return { users: [{id: 1, name: 'bob'}, {id: 2, name: 'bill'}] } } After modifying the data as shown below: this.users[0].name = 'Mary' The watc ...

ajax eliminating any content that comes after a space

When using ajax to populate options in a select field within my form, I noticed that everything after a space gets removed when submitting to the controller. <div class="col-md-9"> <form class="" action="<?php echo base_url(); ?>Sms/add" me ...

How to measure the loading time of a component in Angular 7 and determine when it is fully rendered on the DOM

I am attempting to measure the time it takes for a component to fully load into the DOM. I found a solution that suggests using the following code snippet within the ngOnInit() function from this stackoverflow answer: ngOnInit() { var time = window.pe ...

List of nested objects converted into a flat array of objects

Looking to transform a data structure from an array of objects containing objects to an objects in array setup using JavaScript/Typescript. Input: [ { "a": "Content A", "b": { "1": "Content ...

Verifying if every item in an array exists within multiple arrays

I am struggling to find a solution to this problem. Imagine there are 6 sets of colors with varying amounts of colors in each, and colors may be repeated: ['white', 'blue'] ['green', 'yellow'] ['black'] [ ...

What is the method for incorporating locales into getStaticPaths in Next.js?

I am currently utilizing Strapi as a CMS and dealing with querying for slugs. My goal is to generate static pages using getStaticPaths and getStaticProps in Next.js. Since I'm working with multiple locales, I have to iterate through the locales to re ...

Switching a button's appearance by clicking on a different row

This form contains a toggle edit feature. <% while(resultset.next()){ %> <form method='POST' class="formfield" action='EditCompany'> <tbody> <tr align='center' class='form'> <td><% ...

The website is not delaying for the synchronous web service response

When I call a sync web service on my website, the results are not being waited for. I am calling the function loadExtLayout within the loadLayout function, and then calling loadLayout in other functions on the website. HTTPRequestService.prototype.l ...

Issues with single and double quotation marks in JQuery

I'm having trouble inserting a tag into the page. The tag contains both single and double quotes, so I tried storing that part in a variable named content. Can someone explain why this content variable isn't displaying onclick after the code runs ...

Add MySQL JSON data to an array

Having an issue with a MySQL query in Node.js. After executing the query and receiving a JSON result, I'm trying to store the query result in an array variable. However, I'm having trouble accessing the elements of the array as it always returns ...

The Storybook compilation consistently encounters errors

Check out my repository here: https://github.com/zapify-ui/zapify I'm facing an issue with building Storybook using the command: npm run build-storybook How can I identify the specific error or debug it effectively? The error thrown is: (node:46619) ...

What sets TypeScript apart is the distinction between types like `InstanceType<typeof MyClass>` and `: MyClass`

I have been pondering whether there is a distinction between using InstanceType to declare a type, or simply using the Class name. For instance, considering the following class: MyClass { public static foo: string = 'abc' public makeFoo() ...

Is it possible for the .find() method to extract values from <a> tags?

I'm currently working on retrieving values from an anchor tag using jQuery instead of a button in my HTML. I want to style it with images because a button doesn't match the look of my website. Here's what I've been trying: var mssg_i ...