admin管理员组文章数量:1332896
How can I make my Angular Material formly fields read-only? I've looked at their documentation for props and seen nothing relating to read-only fields.
I Added "readonly: true" as a prop to my Formly fields in my component.ts file and could not see the changes reflected in rendered form.
appponent.ts
import {Component} from '@angular/core';
import {FormGroup} from '@angular/forms';
import {FormlyFieldConfig} from '@ngx-formly/core';
@Component({
selector: 'app',
template: `
<form [formGroup]="form" (ngSubmit)="onSubmit(model)">
<formly-form [form]="form" [fields]="fields" [model]="model"></formly-form>
<button type="submit" class="btn btn-default">Submit</button>
</form>
`,
})
export class AppComponent {
form = new FormGroup({});
model = { email: '[email protected]' };
fields: FormlyFieldConfig[] = [
{
key: 'email',
type: 'input',
props: {
label: 'Email address',
placeholder: 'Enter email',
required: true,
}
}
];
onSubmit(model) {
console.log(model);
}
}
appponent.html
<form [formGroup]="form" (ngSubmit)="onSubmit(model)">
<formly-form [form]="form" [fields]="fields" [model]="model"></formly-form>
<button type="submit" class="btn btn-default">Submit</button>
</form>
How can I make my Angular Material formly fields read-only? I've looked at their documentation for props and seen nothing relating to read-only fields.
I Added "readonly: true" as a prop to my Formly fields in my component.ts file and could not see the changes reflected in rendered form.
appponent.ts
import {Component} from '@angular/core';
import {FormGroup} from '@angular/forms';
import {FormlyFieldConfig} from '@ngx-formly/core';
@Component({
selector: 'app',
template: `
<form [formGroup]="form" (ngSubmit)="onSubmit(model)">
<formly-form [form]="form" [fields]="fields" [model]="model"></formly-form>
<button type="submit" class="btn btn-default">Submit</button>
</form>
`,
})
export class AppComponent {
form = new FormGroup({});
model = { email: '[email protected]' };
fields: FormlyFieldConfig[] = [
{
key: 'email',
type: 'input',
props: {
label: 'Email address',
placeholder: 'Enter email',
required: true,
}
}
];
onSubmit(model) {
console.log(model);
}
}
appponent.html
<form [formGroup]="form" (ngSubmit)="onSubmit(model)">
<formly-form [form]="form" [fields]="fields" [model]="model"></formly-form>
<button type="submit" class="btn btn-default">Submit</button>
</form>
Share
Improve this question
asked Nov 21, 2024 at 5:29
KafrizzKafrizz
212 bronze badges
2 Answers
Reset to default 0In the latest versions of formly
, you can use formState
property of options to pass in the readOnly
property.
options: FormlyFormOptions = {
formState: { readonly: true },
};
Then use this property in the form definitions property expressions
and update the readonly
property.
expressions: {
'props.readonly': 'formState.readonly',
},
I am using signals to achieve the reactive toggling of readOnly
but you can use traditional angular methods. The effect
reacts to the signal changes and updates the options.
Full Code:
import { Component, computed, effect, Signal, signal } from '@angular/core';
import { FormGroup } from '@angular/forms';
import { FormlyFormOptions, FormlyFieldConfig } from '@ngx-formly/core';
@Component({
selector: 'formly-app-example',
templateUrl: './appponent.html',
standalone: false,
})
export class AppComponent {
readOnly = signal(false);
form = new FormGroup({});
model = { email: '[email protected]' };
fields: FormlyFieldConfig[] = [
{
key: 'email',
type: 'input',
expressions: {
'props.readonly': 'formState.readonly',
},
templateOptions: {
label: 'Email address',
placeholder: 'Enter email',
required: true,
},
},
];
computedOptions = {
formState: { readonly: this.readOnly() },
};
constructor() {
effect(() => {
thisputedOptions.formState.readonly = this.readOnly();
});
}
toggle() {
this.readOnly.update((value) => !value);
}
onSubmit() {
alert(JSON.stringify(this.model, null, 4));
}
}
HTML:
<button type="button" class="btn btn-primary" (click)="toggle()">
{{ readOnly() ? 'edit' : 'readOnly' }}
</button>
<form [formGroup]="form" (ngSubmit)="onSubmit()">
<formly-form
[model]="model"
[fields]="fields"
[options]="computedOptions"
[form]="form"
></formly-form>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
Stackblitz Demo
You can use disabled property of anuglar to make the field read-only. refer the code below for reference:
appponent.ts:
import { Component } from '@angular/core';
import { FormGroup } from '@angular/forms';
import { FormlyFieldConfig } from '@ngx-formly/core';
@Component({
selector: 'app',
})
export class AppComponent {
form = new FormGroup({});
model = { email: '[email protected]' };
fields: FormlyFieldConfig[] = [
{
key: 'email',
type: 'input',
props: {
label: 'Email address',
placeholder: 'Enter email',
required: true,
disabled: true, // set disabled property to true field to be read-only
},
},
];
onSubmit(model: any) {
console.log(model);
}
}
appponent.html:
<form [formGroup]="form" (ngSubmit)="onSubmit(model)">
<formly-form [form]="form" [fields]="fields" [model]="model"></formly-form>
<button type="submit" class="btn btn-default">Submit</button>
</form>
本文标签: formsAngular Formly Read OnlyStack Overflow
版权声明:本文标题:forms - Angular Formly Read Only - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742312307a2451117.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论