Can you do a get(<path>) on an UntypedFormGroup if there is a '.' in the path parameter?

Discussão em 'Angular' iniciado por Tim, Novembro 12, 2024.

  1. Tim

    Tim Guest

    I have an Angular app that uses an UntypedFormGroup to store and retrieve input values from the user.

    I add controls to the UntypedFormGroup using the addControl function like so:

    this.myUntypedFormGroup.addControl('mypath', new UntypedFormControl());


    and then retrieve the value like so:

    this.myUntypedFormGroup.get('mypath').value


    This works fine - except if I want to use a period in the path parameter.

    For example, if I want the path to be the name of a pdf file:

    this.myUntypedFormGroup.addControl('myfile.pdf', new UntypedFormControl());


    It comes back with a null pointer exception when I try to retrieve it:

    this.myUntypedFormGroup.get('myfile.pdf').value


    I think I sort of understand why this is - it is interpreting the period in mypdffile.pdf to denote that 'pdf' is supposed to be a sub-control of 'myfile' and therefore it dies when it can't find a sub-control called 'pdf'.

    Is there any way to do an escape on the period character such that it takes it as a literal part of the path string rather than assuming there is a sub-control?

    Thanks much.

    Continue reading...

Compartilhe esta Página