0

I have accessed to a variable called key in this html element. How do I put the key inside of the

*ngIf below: *ngIf="isSubmitted && errors.key.translations", especially the errors.key.translation part.

<div class="form-group col" *ngFor="let key of matchingKeys">
    <div
        *ngIf="isSubmitted && errors.key.translations"
        class="invalid-feedback"
    >       
</div>
5
  • Do you mean access whatever isSubmitted && errors.key.translations gives inside the div? Commented Feb 24, 2020 at 15:02
  • No, I mean that errors.key.translations does not work. What is the correct syntax? Commented Feb 24, 2020 at 15:04
  • 2
    Try errors[key].translations Commented Feb 24, 2020 at 15:05
  • @AntonÖdman that syntax is correct. Are you getting any error? Commented Feb 24, 2020 at 15:07
  • Michael's snippet should work fine: errors[key].translations Commented Feb 24, 2020 at 15:17

3 Answers 3

2

Replace dot with brackets. It should allow to access properties by name stored in a variable. Refer here. The following code should do it

<div *ngFor="let key of matchingKeys">
  <div *ngIf="isSubmitted && errors[key].translations">
    <p>
      {{ key }}
    </p>
  </div>       
</div>

Working example: Stackblitz

Sign up to request clarification or add additional context in comments.

Comments

0

Try this:

<div class="form-group col" *ngFor="let key of matchingKeys">
    <div
        *ngIf="isSubmitted && errors[key]['translations']"
        class="invalid-feedback"
    >       
</div>

Comments

0

I am assuming matchingKeys are the keys to get error..

Try:

*ngIf="isSubmitted && errors[key].translations"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.