1
votes

I want to map value of my hero object from Reactive form, but it doesn't work.

Here is my hero-add.component.ts:

import { Component, OnInit, Input } from '@angular/core';
import { Hero } from '../Hero';
import { HeroService } from '../hero.service';
import { Router } from '@angular/router';
import { FormBuilder, FormGroup, Validators, ReactiveFormsModule } from
    '@angular/forms';

@Component({
    selector: 'app-hero-add',
    templateUrl: './hero-add.component.html',
    styleUrls: ['./hero-add.component.css']
})
export class HeroAddComponent implements OnInit {
    hero: Hero = new Hero();
    myForm = new FormGroup({}) // Instantiating our form

    get f() { return this.myForm.controls; }

    constructor(private heroService: HeroService, private router: Router,
        private formBuilder: FormBuilder) {
        this.myForm = formBuilder.group({
            publishedYear: [this.hero.Name, [Validators.required,
            Validators.min(1990), Validators.max(2018)]],//I expect this will map value of name from form to my this.hero.Name
            name: [this.hero.PublishedYear, [Validators.required,
            Validators.minLength(2)]] //I expect this will map value of publishedDate from form to my this.hero.Published
        });
    }

    ngOnInit() {
    }

    addHero() {
        var hr = this.hero;//hr= Hero {} because this.hero = Hero{} 
    }
}

Here is my hero-add.component.html:

 <div>
    <form [formGroup]="myForm">
        <div class="name">
            <label for="name">Name: </label>
            <input type="text" id="Name" formControlName="name">
            <p class="alert alert-danger invalid" *ngIf="f.name.errors">Invalid Name</p>
        </div>

        <div class="publishedYear">
            <label for="publishedYear">Publish Year: </label>
            <input type="number" id="PublishedYear" formControlName="publishedYear">
            <p class="alert alert-danger invalid" *ngIf="f.publishedYear.errors">Invalid Published Year</p>
        </div>
    </form>

    <div class="complete">
        <label>Complete</label>
        <input type="radio" id="Complete" name="Complete" [(ngModel)]="hero.Complete" value="true">Yes
        <input type="radio" id="Complete" name="Complete" [(ngModel)]="hero.Complete" value="false">No
    </div>

    <button (click)='addHero()'>Add new</button>
</div>

I input the field name and published year, then I click button "Add hero" to go to function addHero(). I set a break point in this function and the value of this.Hero is empty. I don't understand why the values don't map from form to my object.

How can I solve the problem?

1

1 Answers

2
votes

your function addHero must be like

addHero()
{
  if (this.myform.valid)
  {
      var hr=this.myForm.value;
  }
}

Or even better, you change your .html and pass as variable the form

<button (click)='addHero(myForm)'>Add new</button>
//And addHero receive as argument
addHero(form)
{
  if (form.valid)
  {
      var hr=form.value;
  }
}

One thing is the "hero data" (your variable hero), another is the "hero form" your variable "myForm"