最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How pass input value as FormGroup in Angular 17+ - Stack Overflow

programmeradmin7浏览0评论

I'm trying to use Signals Input in angular 17 to pass a specific form. In the older approach works fine:

@Component({
  selector: 'app-user-data-create-update',
  standalone: true,
  imports: [ReactiveFormsModule, PanelModule, NgStyle],
  templateUrl: './user-data-create-updateponent.html',
  styleUrl: './user-data-create-updateponent.scss',
})
export class UserDataCreateUpdateComponent {
  // Injects
  private readonly formBuilder: FormBuilder = inject(FormBuilder);

  // Inputs
  @Input() form: Signal<FormGroup<any>>; //works fine
  fontSize = input<string>('0.8rem'); //works fine

  constructor() {
    this.form = signal(
      this.formBuilder.group({
        createdDateTime: [''],
        creatorXID: [''],
        creatorPersonXID: [''],
        creatorPersonEmail: [''],
        updatedDateTime: [''],
        updaterXID: [''],
        updaterPersonXID: [''],
        updaterPersonEmail: [''],
      })
    );
  }
}

Calling component:

<app-user-data-create-update [form]="formGroupProgram"/>

However, I've already tried a lot of things to works with "new" Angular signals with FormGroup and didn't works.

Any idea?

I'm trying to use Signals Input in angular 17 to pass a specific form. In the older approach works fine:

@Component({
  selector: 'app-user-data-create-update',
  standalone: true,
  imports: [ReactiveFormsModule, PanelModule, NgStyle],
  templateUrl: './user-data-create-updateponent.html',
  styleUrl: './user-data-create-updateponent.scss',
})
export class UserDataCreateUpdateComponent {
  // Injects
  private readonly formBuilder: FormBuilder = inject(FormBuilder);

  // Inputs
  @Input() form: Signal<FormGroup<any>>; //works fine
  fontSize = input<string>('0.8rem'); //works fine

  constructor() {
    this.form = signal(
      this.formBuilder.group({
        createdDateTime: [''],
        creatorXID: [''],
        creatorPersonXID: [''],
        creatorPersonEmail: [''],
        updatedDateTime: [''],
        updaterXID: [''],
        updaterPersonXID: [''],
        updaterPersonEmail: [''],
      })
    );
  }
}

Calling component:

<app-user-data-create-update [form]="formGroupProgram"/>

However, I've already tried a lot of things to works with "new" Angular signals with FormGroup and didn't works.

Any idea?

Share Improve this question edited Mar 27 at 20:17 jonrsharpe 122k30 gold badges268 silver badges476 bronze badges asked Mar 19 at 14:45 devinvestidordevinvestidor 1231 silver badge5 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

Angular 17 & 18:

Please initialize the default value inside the function input.

We can use the input signal to behave the same as @Input and receive a new value from the parent, or use the value initialized inside the input signal initialization.

@Component({
  selector: 'app-user-data-create-update',
  standalone: true,
  imports: [ReactiveFormsModule, PanelModule, NgStyle],
  templateUrl: './user-data-create-updateponent.html',
  styleUrl: './user-data-create-updateponent.scss',
})
export class UserDataCreateUpdateComponent {
  // Injects
  private readonly formBuilder: FormBuilder = inject(FormBuilder);

  // Inputs
  readonly form: InputSignal<FormGroup<any>> = input(this.getForm()); //works fine
  readonly fontSize = input<string>('0.8rem'); //works fine

  getForm() {
    return this.formBuilder.group({
        createdDateTime: [''],
        creatorXID: [''],
        creatorPersonXID: [''],
        creatorPersonEmail: [''],
        updatedDateTime: [''],
        updaterXID: [''],
        updaterPersonXID: [''],
        updaterPersonEmail: [''],
    });
  }
}

Angular 19:

If you want the form state to change in the future, meaning you will override the value with something else locally in your component, then you require something called local state (linkedSignal - will take a value from the signals inside and can also be modified by the user unlike input signal).

Here we initialize the input with a different name, but since the different name will affect data binding, we use alias to set the data binding name as form

@Component({
  selector: 'app-user-data-create-update',
  standalone: true,
  imports: [ReactiveFormsModule, PanelModule, NgStyle],
  templateUrl: './user-data-create-updateponent.html',
  styleUrl: './user-data-create-updateponent.scss',
})
export class UserDataCreateUpdateComponent {
  // Injects
  private readonly formBuilder: FormBuilder = inject(FormBuilder);

  // Inputs
  readonly formInput: InputSignal<FormGroup<any>> = input(this.getForm(), { alias: 'form' }); //works fine
  readonly form: WritableSignal<FormGroup<any>> = linkedSignal(() => this.formInput());
  readonly fontSize = input<string>('0.8rem'); //works fine

  getForm() {
    return this.formBuilder.group({
        createdDateTime: [''],
        creatorXID: [''],
        creatorPersonXID: [''],
        creatorPersonEmail: [''],
        updatedDateTime: [''],
        updaterXID: [''],
        updaterPersonXID: [''],
        updaterPersonEmail: [''],
    });
  }

  ngAfterViewInit() {
    this.form.set(  // <- you can change the form state locally if needed
      this.formBuilder.group({
        createdDateTime: [''],
        creatorXID: [''],
      })
    );
  }
}
发布评论

评论列表(0)

  1. 暂无评论