2

I'm working on my web-app and I am facing a problem.

I have an array with several values, which I'd like to display in the frontend as list or something similar.

app.component.ts

in this function I split the tags from the string into an array

splitTags() {
  if (this.data.tags != null) {
    var tag = this.data.tags.split(";")
    console.log(tag)
  }
}

ngOnInit() {
  this.splitTags()
}

app.component.html

here I d'like to display the tags in a list

<li *ngFor="let tag in tags">
  {{ tag }}
</li>

but nothing appears, also if I see the values in the console.

3
  • 3
    You need to use of not in: <li *ngFor="let tag of tags"> Commented Apr 8, 2020 at 8:10
  • @AndreiTătar yes and there is another error he don't store the result of split method in a componenet property 👍 Commented Apr 8, 2020 at 8:15
  • where is your "tags" property in the app.component.ts ? Commented Apr 8, 2020 at 8:18

1 Answer 1

1

you need to create a property to hold the split result

tags:any[]; // 1️⃣

splitTags() {
  if (this.data.tags != null) {
    this.tags = this.data.tags.split(";"); // 2️⃣
    console.log(this.tags)
  }
}

ngOnInit() {
  this.splitTags()
}


template

<li *ngFor="let tag of tags">
  {{ tag }}
</li>
Sign up to request clarification or add additional context in comments.

2 Comments

@malbaravi - I've an other question: I added now a limit of 5 in my list. But sometimes there are more than 5 values in that array. How can I add a button "show all" and then display everything in that list?
@macintosh11 check this demo stackblitz.com/edit/angular-sbck7s may help 👍

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.