Giter VIP home page Giter VIP logo

Comments (2)

CSSinghNet avatar CSSinghNet commented on April 28, 2024

The issue you're facing is due to the fact that you're reassigning a new form group to this.form every time ngOnChanges is triggered, which causes the checkbox to be disabled even when this.item.name is not equal to 111.

To fix this, you should only create the form group if it hasn't been created yet. Additionally, you should update the form controls based on the changes in the input data. Here's an updated version of your code:

@component({
selector: "form-test",
template: <form [formGroup]="form"> <input type="checkbox" formControlName="checkbox" />{{ item.name }} <p>form checkbox disabled: {{ form.get("checkbox").disabled }}</p> </form>,
})
export class FormTestComponent implements OnChanges {
@input() item: any;

form: FormGroup;

constructor(private fb: FormBuilder) {
// Initialize the form once in the constructor
this.form = this.fb.group({
name: [""],
checkbox: [false],
});
}

ngOnChanges(changes: SimpleChanges): void {
// Check if the form has been initialized
if (this.form) {
// Update the form controls based on the input changes
this.form.patchValue({
name: this.item.name,
checkbox: this.item.name === 111 ? true : false,
});

  // Disable the checkbox if this.item.name is 111
  if (this.item.name === 111) {
    this.form.get("checkbox")?.disable();
  } else {
    this.form.get("checkbox")?.enable();
  }
}

}
}

Preview

from angular.

Vibing avatar Vibing commented on April 28, 2024

The issue you're facing is due to the fact that you're reassigning a new form group to this.form every time ngOnChanges is triggered, which causes the checkbox to be disabled even when this.item.name is not equal to 111.

To fix this, you should only create the form group if it hasn't been created yet. Additionally, you should update the form controls based on the changes in the input data. Here's an updated version of your code:

@component({ selector: "form-test", template: <form [formGroup]="form"> <input type="checkbox" formControlName="checkbox" />{{ item.name }} <p>form checkbox disabled: {{ form.get("checkbox").disabled }}</p> </form>, }) export class FormTestComponent implements OnChanges { @input() item: any;

form: FormGroup;

constructor(private fb: FormBuilder) { // Initialize the form once in the constructor this.form = this.fb.group({ name: [""], checkbox: [false], }); }

ngOnChanges(changes: SimpleChanges): void { // Check if the form has been initialized if (this.form) { // Update the form controls based on the input changes this.form.patchValue({ name: this.item.name, checkbox: this.item.name === 111 ? true : false, });

  // Disable the checkbox if this.item.name is 111
  if (this.item.name === 111) {
    this.form.get("checkbox")?.disable();
  } else {
    this.form.get("checkbox")?.enable();
  }
}

} }

Preview

Thank you for your answer. But I don't think it's a good solution, it's more like avoidance. I'm using the exact same code in Angular 15 and Angular 17 is just right, it's more in line with the developer's thinking.

Here's the demo using v17

from angular.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.