Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using more than one @input() decorator - ionic 4

Tags:

angular

ionic4

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>`
like image 355
Matheus Batista Avatar asked Jun 07 '26 03:06

Matheus Batista


1 Answers

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.

like image 175
Fateme Fazli Avatar answered Jun 10 '26 18:06

Fateme Fazli



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!