I have an additional component for a button that needs to be incorporated in various other components containing forms. The button always stays the same, with consistent style formatting. The only variable is the function(s) that should be triggered upon clicking the button depending on the process.
For example, in user-login.html, I would trigger:
, whereas in (click)="login(inputUserName.value, inputUserPass.value)"
user-register.html
, I would trigger: (click)="register(inputUserEmail.value)"
While attempting to implement this concept:
button-form.component.html:
<button mat-raised-button [disabled]="!form.valid" [color]="'primary'">
<span>{{buttonText}}</span>
</button>
button-form.component.ts
:
import { Component, OnInit, Input } from '@angular/core';
@Component({
selector: 'app-button-form',
templateUrl: './button-form.component.html'
})
export class ButtonFormComponent implements OnInit {
@Input() buttonText: string;
constructor() { }
ngOnInit() {
}
}
user-login.html:
<form #form="ngForm">
....
<app-form-button [buttonText]="TextToShowOnButton"></app-form-button>
....
</form>
I encounter two challenges:
With regards to the
disabled
property, I receive
. This error is understandable as the propertyERROR TypeError: Cannot read property 'valid' of undefined
form
is not defined inbutton-form.component.ts
, while the form exists inlogin.html
as<form #form="ngForm">
. I attempted using interpolation within property binding, but it did not resolve the issue.I am struggling to dynamically pass the function to the button based on the form and the process.
I have researched this matter extensively but haven't discovered a suitable solution...