0

I'm stuck with Angular5 ReactiveForm.
Actually, I create a basic model sub

sub.model.ts :

export interface Subscription {
    name: string;
}

I also have a component : : sub.component.ts

import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';

import { Sub } from '@models/sub.model';

@Component({
    selector: 'md-subscribe',
    templateUrl: './subscribe.component.html',
    styleUrls: ['./subscribe.component.scss']
})
export class SubComponent {

    subForm: FormGroup;
    sub: Subscription;

    constructor(private builder: FormBuilder) {
        this.subForm = this.builder.group({
            name : ['', Validators.required]
        });
    }

    subscribe() {
        this.sub.name = 'test value';
    }

}

And to conclude, a html template :

<form class="form align-end" [formGroup]="subscribeForm" (ngSubmit)="subscribe()">
    <h2 class="label label--blue">S'inscrire</h2>
    <div class="form-group"><input type="text" id="name" name="name" formControlName="name" placeholder="Hubert" required /></div>
</form>

So I try to submit the form. I can get the value with this.subForm.value but when I try to assign value test value or this.subForm.value.name, I have an error in console.

ERROR TypeError: Cannot set property 'email' of undefined
    at SubscribeComponent.subscribe (subscribe.component.ts:24)
    at Object.eval [as handleEvent] (SubscribeComponent.html:1)
    at handleEvent (core.js:13581)
    at callWithDebugContext (core.js:15090)
    at Object.debugHandleEvent [as handleEvent] (core.js:14677)
    at dispatchEvent (core.js:9990)
    at eval (core.js:12332)
    at SafeSubscriber.schedulerFn [as _next] (core.js:4351)
    at SafeSubscriber.__tryOrUnsub (Subscriber.js:240)
    at SafeSubscriber.next (Subscriber.js:187)

In fact, my object sub is undefined but why I can't assign value. I also changed zone.js version but problem still. How can I assign value ?

3
  • Can you show us subscribe.component.ts? Commented Jan 19, 2018 at 9:27
  • where is the property email in the code Commented Jan 19, 2018 at 9:35
  • It's not email but name. I change the code and subscribe component is the sub component. Same thing Commented Jan 19, 2018 at 9:48

2 Answers 2

1

When I'm working with ReactiveForms, I like have two variables:data and dataForm (in your case sub and subForm) and a function buildForm

subForm: FormGroup;
sub: Subscription;

constructor(private builder: FormBuilder) {}
//For example in ngOnInit
ngOnInit()
{
     this.buildForm();
}
//or when we subscribe
subscribe() {
    this.sub.name = 'test value';  //<--give value to the "data"
    this.subForm=this.buildForm(this.sub);  //<--create a groupForm with the data
}
//Our function buildForm return a formGroup
buildForm(sub:Subscription|null)
{
   return this.builder.group({
      name : [sub? sub.name:'', Validators.required]
   })
}
Sign up to request clarification or add additional context in comments.

2 Comments

You don't do a mistake with this.subForm.buildForm() ?
thanks!!, I update the post (there was another error, is this.buildForm in subscribe
1

I think it is because you are using wrong object. You have only declaration of sub. In your componentshould be:

sub: Subscription = <Subscription>{name:""} ;
subscribe() {
    this.sub.name = this.subForm.value.name;
}

Here is example without error https://stackblitz.com/edit/angular-atjwke

5 Comments

Yeah it works but i want assigned the form value on a variable and not a variable on form value. To test the code, i put placeholder variable in sub object
stackblitz.com/edit/angular-atjwke check updated example you must initialize you sub instance
It works !! Thanks. But code is very charged. You don't know why i have this problem. I can't assign new value on undefined var
"Yeah it works but i want assigned the form value on a variable and not a variable on form value." You had this problem because if you want assing propery on any object this object must exist even like let obj ={}; ERROR TypeError: Cannot set property 'email' of undefined - this is no problem with form, or reactive form. You have object which is undefined and you are using it's property
Thank you for your explications. I close this topic.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.