forms - How to add [disabled]='!f.valid' to submit button using ng-content in angular 4? -
have form component contains email, password field , submit button passed through ng-content login form 'login' button can showed , register form 'register' button can showed. form component template code:
<form #f="ngform" (ngsubmit)="onsubmit(f.value)"> <div class="form-group"> <label for="email">email</label> <input type="email" class="form-control" placeholder="enter email address" name="email" ngmodel > </div> <div class="form-group"> <label for="password">password</label> <input type="password" class="form-control" placeholder="enter password" name="password" ngmodel > </div> <ng-content select="[submitbtn]" ></ng-content>
code of form's parent component:
<div class="row"> <div class="col-md-6"> <!--for login--> <app-form-content> <button type="submit" class="btn btn-default" submitbtn>login</button> </app-form-content> </div> <!--for register--> <div class="col-md-6"> <app-form-content> <button type="submit" class="btn btn-default" [disabled]="!f.valid" submitbtn>register</button> </app-form-content> </div> </div>
after run code "typeerror: cannot read property 'valid' of undefined" error occured. how resolve error?
you can't use template reference variable #f
in parent template because scope entire template declared.
in order access variable within parent template suggest declare property inside form component:
@viewchild('f') f: ngform;
after can property parent template reference #formcontent
in parent template like:
<app-form-content #formcontent> <button type="submit" class="btn btn-default" [disabled]="!formcontent.f.valid"
Comments
Post a Comment