2

I would like to use a component as an item.
Instead of using:

<ul class='biblList'>
    <li *ngFor="let biblCit of biblCits">
        {{ biblCit }}
    </li>
</ul>

I would like to use something like:

<evt-bibliography-item *ngFor="let biblCit of biblCits">
    {{ biblCit }}
</evt-bibliography-item>

Being a novice with angular I don't know how to figure it out.

3
  • 2
    What did you already tried? Commented Dec 16, 2019 at 9:21
  • I tried creating an empty component and calling it with a tag, but it is clearly incomplete code. Commented Dec 16, 2019 at 9:25
  • 1
    stackoverflow.com/questions/35562626/… Commented Dec 16, 2019 at 9:34

4 Answers 4

1

here is working example

.ts

biblCits=['1','2','2','3','4','5']

.html

<div *ngFor="let biblCit of biblCits">
    <child-component [parentTimerCount]="biblCit">
    </child-component>
</div>
Sign up to request clarification or add additional context in comments.

Comments

0

you can use <ng-content></ng-content> at the component template and this will project the template from the parent

bibliography component template

<div>
<ng-content></ng-content>
</div>

parent

<app-bibliography *ngFor="let biblCit of biblCits">
        👉 {{ biblCit }} 
</app-bibliography>

demo 🚀

3 Comments

@downvoter what's reason to downvote? It would be really helpful to improve my reply
For me it was the right solution! I had to read between the lines, but in the end I realized it was what I was looking for!
@Memmo 🙂 happy to hear that my answer solve your problem , thanks 👍
0

You can use <ng-content></ng-content> inside of your evt-bibliography-item component's template to access everything passed between your own component tags, in your case the value of {{ biblCit }}

@Component({
  selector: 'evt-bibliography-item',
    template: '<ng-content></ng-content>'
})
export class BibliographyItem {
}

Comments

0

You can use iterator *ngFor to iterate through components:

<ng-content *ngFor="let biblCit of biblCits">
  <evt-bibliography-item></evt-bibliography-item>
</ng-content>

5 Comments

@downvoter what's reason to downvote? It would be really helpful to improve my reply
evt-bibliography-item is not a known element
@Memmo it looks like you've not imported your component.
in app.module or elsewhere?
if it is standalone component, then yeah, import into app.module.ts

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.