Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <form [formGroup]="form">
- <input type="text" id="name" formControlName="name" />
- <input type="text" id="age" formControlName="age" />
- </form>
- ngOnInit() {
- this._route.queryParams.subscribe(params => {
- getUser(params.userId).subscribe(user => {
- this._buildForm(user);
- })
- });
- }
- private _buildForm(user){
- this.form = _fb.group({
- name: [{value: user.name, disabled: user.someCondition}, Validators.required],
- age: [{value: user.age, disabled: user.anotherCondition}, Validators.required]
- })
- }
- this.form.disable() //Doesn't do anything
- this.form.controls.name.disable(); //Doesn't help
- <button (click)="form.disable()" value="Disable Form" />
- <button (click)="form.enable()" value="Enable Form" />
- this._router.navigate([], {
- relativeTo: this._route,
- queryParams: {userId: 10}
- })
- @Component({
- selector: 'my-app',
- templateUrl: './app.component.html',
- styleUrls: [ './app.component.css' ]
- })
- export class AppComponent {
- group;
- isDisabled = true;
- constructor(fb: FormBuilder) {
- this.group = fb.group({
- stuff: fb.control({value: 'stuff', disabled: this.isDisabled})
- });
- }
- toggle() {
- this.isDisabled = !this.isDisabled;
- this.group.controls.stuff[this.isDisabled ? 'enable' : 'disable']();
- }
- }
- <form [formGroup]="form" (submit)="_updateForm()">
- <input type="text" id="name" fromControlName="name" />
- <input type="text" id="age" fromControlName="age" />
- </form>
- private userId: any;
- ngOnInit() {
- this._route.queryParams.subscribe(params => {
- this.userId = params.userId;
- this._updateForm();
- });
- }
- private _buildForm(user){
- this.form = _fb.group({
- name: [{value: user.name, disabled: user.someCondition}, Validators.required],
- age: [{value: user.age, disabled: user.anotherCondition}, Validators.required]
- });
- }
- // The update function has to be public to be called inside your template
- public _updateForm(){
- getUser(this.userId).subscribe(user => {
- this._buildForm(user);
- });
- }
- <form [formGroup]="form">
- <input type="text" id="name" fromControlName="name" />
- <input type="text" id="age" fromControlName="age" />
- </form>
Add Comment
Please, Sign In to add comment