angular - Angular2: Form values not picked up -
i've created form inside ngoninit()
method:
this.cardform = this.fb.group({ card_number: ['', validators.required], holdername: ['', validators.required], expiry: ['', validators.required], cvc: ['', validators.required], address_line1: ['', validators.required], address_line2: ['', validators.required], address_city: ['', validators.required], address_state: ['', validators.required], address_zip: ['', validators.required], address_country: ['', validators.required] });
on template:
<form [formgroup]="cardform" novalidate="novalidate"> <div class="form-group"> <label for="cardnumber">card number</label> <input type="text" name="cardnumber" class="input-transparent form-control" formcontrolname="card_number" data-mask="9999-9999-9999-9999" placeholder="____-____-____-____" required="required"> </div>
when i'm trying values of form, card_number
, cvc
, expiry
empty.
as can see, cardform
have several fields. i've binded in 2 <from>
tags on tamplate.
so, card_number, holdername, expiry , cvc
binded on first form:
<form [formgroup]="cardform" novalidate="novalidate"> <input id="card_number" type="text" name="card_number" class="input-transparent form-control" formcontrolname="card_number"...
and other ones binded on second <form>
tag:
<form [formgroup]="cardform" novalidate="novalidate"> <input id="address_line1" type="text" name="address_line1" class="input-transparent form-control" formcontrolname="address_line1"...
i've tried create 2 formgroup
, , bind them each form on template:
this.cardform = this.fb.group({ card_number: ['', validators.required], holdername: ['', validators.required], expiry: ['', validators.required], cvc: ['', validators.required] }); this.detailsform = this.fb.group({ address_line1: ['', validators.required], address_line2: ['', validators.required], address_city: ['', validators.required], address_state: ['', validators.required], address_zip: ['', validators.required], address_country: ['', validators.required] });
on template:
<form [formgroup]="cardform" novalidate="novalidate"> <form [formgroup]="detailsform" novalidate="novalidate">
nevertheless:
for similar case used patchvalue
or setvalue
of particular formgroup object.
for e.g in ngoninit
you can set:
this.cardform.patchvalue({card_number: 'foobar'});
again, if value changes can update in similar way.
carddetailschanged(val){ this.cardform.patchvalue({card_number: val}); }
Comments
Post a Comment