0

I am trying to pass data array object to html list dynamically.

 <div class="sidebar" *ngFor="let q of obj">
        <h4>Approvals</h4>

        <ul  *ngFor="let c of q.element">
            <li>
                <a href="#"><img class="sideico" src="../../../assets/images/icons/BRIA.svg" />{{c}}</a></li>
        </ul>
</div>

In ts file

 obj = [
          {
            "element" : "Timesheet",
            "icon": "../../../assets/images/icons/BRIA.svg"
          },
          {
            "element" : "Timesheet",
            "icon": "../../../assets/images/icons/BRIA.svg"
          }
];

I am new in Angular. What is wrong?

4
  • your object doesnt include name q.name? Commented Jul 20, 2020 at 14:04
  • q.element is a string you can't use ngFor for that. What is the aim of second for loop? @OM-ॐ Commented Jul 20, 2020 at 14:07
  • Please elaborate your requirement properly. Commented Jul 20, 2020 at 14:09
  • Guys this is done i have marked the answer Commented Jul 20, 2020 at 15:02

2 Answers 2

1

Demo You need just one loop

 <div class="sidebar" >
     <h4>Approvals</h4>
     <ul  *ngFor="let q of obj">
        <li>
            <a href="#"><img class="sideico" [src]="q.icon | safe : 'url'" />{{q.element}}</a>  
        </li>
    </ul>
           
</div>

and use custom safe pipe to bind

import { Pipe, PipeTransform } from '@angular/core';
import { DomSanitizer, SafeHtml, SafeStyle, SafeScript, SafeUrl, SafeResourceUrl } from '@angular/platform-browser';

@Pipe({
  name: 'safe'
})
export class SafePipe implements PipeTransform {

  constructor(protected sanitizer: DomSanitizer) {}
 
 public transform(value: any, type: string): SafeHtml | SafeStyle | SafeScript | SafeUrl | SafeResourceUrl {
    switch (type) {
            case 'html': return this.sanitizer.bypassSecurityTrustHtml(value);
            case 'style': return this.sanitizer.bypassSecurityTrustStyle(value);
            case 'script': return this.sanitizer.bypassSecurityTrustScript(value);
            case 'url': return this.sanitizer.bypassSecurityTrustUrl(value);
            case 'resourceUrl': return this.sanitizer.bypassSecurityTrustResourceUrl(value);
            default: throw new Error(`Invalid safe type specified: ${type}`);
        }
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

You are supposed to iterate the list item not the whole list which is wrong.
1

There is no need to iterate it twice. You just need to iterate it once as shown below:

 <div class="sidebar">
        <h4>Approvals</h4>

        <ul>
            <li  *ngFor="let q of obj">
                <a href="#"><img class="sideico" [src]="q?.icon" />{{ q?.element }}</a>
            </li>
        </ul>
</div>

4 Comments

why q? ?? Can you explain .
Google safe navigation operator. This will help you if the data is undefined or the object does not exist.
you dont need safe operation there. U are already in loop array if array is null you never enter there. Safe operations is good for object binding.
Same will go for src ?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.