Comments (2)
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();
}
}
}
}
from angular.
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(); } }
} }
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.
from angular.
Related Issues (20)
- broken link to lifecycle hooks HOT 1
- Add typing for NgElement Outputs
- Unit test a service that uses effect and injects ChangeDetectorRef HOT 5
- DOM nodes duplicated / not removed ONLY in production configuration HOT 8
- Docs: don't recommend using `$index` as a good default HOT 5
- Signal components: Why call the constructor before the required inputs and static child queries are ready? HOT 4
- Typo on angular.dev HOT 2
- Broken link in the roadmap page HOT 2
- Conflicting paragraph in `Component host elements` doc page HOT 2
- Angular 17 with SSR injection server response is null, when you use response.status. ERROR: Cannot read properties of null (reading 'status') HOT 2
- ISO week-numbering year incorrect for some years HOT 10
- angular.dev documentation has wrong sentence HOT 2
- Broken link on @angular/router RunGuardsAndResolvers HOT 3
- live example of Building a template-driven form is not completed HOT 2
- click events propagation's problem with buttons wrapped into <label> HOT 9
- Unit test with signals fail HOT 2
- docs: Repeated word `when` HOT 3
- Angular dosen't let me use localStorage inside a injection token HOT 5
- Change detection not triggered when Signal.set() called from Observable subscription HOT 4
- signal updates and `markForCheck` should always schedule change detection
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from angular.