Reputation: 305
I'm running a ionic 4 app
I created a message component, which loads accordingly to the error passed as @Input
to it.
<control-messages [control]="saveUserForm.get('age')"></control-messages>
so it loads the FormControl
validation and displays it's custom message as defined by me in it's service;
What I wanted to know, is if there's some way to send more than one property to the component, I wanted to dynamically decorate the <p class='help'></p>
with a success | warn | danger
This is the component:
@Component({
selector: 'control-messages',
template: `<p class="help" *ngIf="errorMessage !== null">{{errorMessage}}</p>`
})
export class ControlMessagesComponent {
@Input() control: FormControl;
constructor() { }
get errorMessage() {
for (const propertyName in this.control.errors) {
if (this.control.errors.hasOwnProperty(propertyName) && this.control.touched) {
return ValidationService.getValidatorErrorMessage(propertyName, this.control.errors[propertyName]);
}
}
return null;
}
}
How can I sent trough the parent another param to load in it's template as {{color}}
, like
template: `<p class="help {{color}}" *ngIf="errorMessage !== null">{{errorMessage}}</p>`
Upvotes: 2
Views: 514
Reputation: 2632
You can add as many inputs as you want to e.g:
@Component({
selector: 'control-messages',
template: `<p class="help" [style.color]="color" *ngIf="errorMessage !== null">{{errorMessage}}</p>`
})
export class ControlMessagesComponent {
@Input() control: FormControl;
@Input() color: string;
...
Usage:
<control-messages
[control]="saveUserForm.get('age')"
color="red">
</control-messages>
Upvotes: 4
Reputation: 11982
You can set input as many as you want:
parent html:
<control-messages
[control]="saveUserForm.get('age')"
color="success">
</control-messages>
child ts:
export class ControlMessagesComponent {
@Input() control: FormControl;
@Input() color: string;
child html:
template: `<p class="help {{color}}" *ngIf="errorMessage !== null">{{errorMessage}}</p>`
But if you are checking the success | warn | danger
from your formControl there is no need to send it from parent.
Upvotes: 6