2

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>`
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Matheus Batista
  • 305
  • 1
  • 3
  • 12

2 Answers2

6

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.

Fateme Fazli
  • 11,582
  • 2
  • 31
  • 48
  • 1
    Why does the first decorator goes with `[]`, and the second one doesn't? I believe that was the mistake I was making – Matheus Batista Nov 06 '19 at 16:23
  • 1
    decorators doesn't relate each other. – Fateme Fazli Nov 06 '19 at 16:30
  • 1
    @MatheusBatista because `[color]=` expects a object and just a plain input like `color` expects a string. But when you cann still use it with `[]` like this `[color]="'red'"` – Norbert Bartko Nov 06 '19 at 16:34
  • 1
    @MatheusBatista Thank you, I did'n understand his question. see https://stackoverflow.com/questions/43633452/when-to-use-square-brackets-in-directives-inputs-and-when-not – Fateme Fazli Nov 06 '19 at 16:36
4

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>
Norbert Bartko
  • 2,468
  • 1
  • 17
  • 36