Angular dynamic value for href in div

I am trying to implement dynamic submenu by Angular.

Currently, I set href by hardcode like below:

<ng-template #preSelectionMenuItem let-preSelections="preSelections">
    <div class='panel-body'>
        <ul class='project-tree-menu__panel-list' [attr.data-guid]="menuGuid">
            <li *ngFor="let preSelection of preSelections">
                <div class='panel-group'>
                    <div *ngIf="preSelection.subPreSelectionDtos.length>0" class='panel panel-primary'>
                        <div class='panel-heading collapsed panel-title estimate-panel-list__title' data-toggle='collapse'
                           href="#1"
                            >
                            {{ preSelection.text | translate}}
                        </div>
                        <div id="1" class='estimate-panel panel-collapse collapse'>
                            <ng-container *ngTemplateOutlet='preSelectionMenuItem;context:{preSelections:preSelection.subPreSelectionDtos}'></ng-container>
                        </div>
                    </div>
                    <div *ngIf="preSelection.subPreSelectionDtos.length==0" class='panel panel-primary'>
                        <div class='estimate-panel-list__option project-tree-menu__item'
                           href="#1"
                            >
                            {{ preSelection.text | translate}}
                        </div>
                    </div>
                </div>
            </li>
        </ul>
    </div>
</ng-template>

Actually, the value #1 should be the preSelection.text which will be able to make sure referrence the correct subitems.

But If I change #1 to href="#{{preSelection.text}}", it will throw

Can’t bind to ‘href’ since it isn’t a known property of ‘div’

Current UI
Angular dynamic value for href in div

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

you can do like this..

<div
  style="margin:0.5em;"
  *ngFor="let preSelection of preSelections; let i = index">
 <div>
   <div
    (click)="onSubmenuClick(i)"
      style="background-color:'gray'; color:'white'; cursor:'pointer' ">
    <span> {{ preSelection.text }} </span>
    <span> + </span>
   </div>
   <ng-container *ngIf="clickedMenu === i">
     <div *ngFor="let subMenu of preSelection.childMenu">
       <div>{{ subMenu.menuText }}</div>
     </div>
   </ng-container>
  </div>
</div>

and inside your component like this..
clickedMenu;
onSubmenuClick(index: number) {

  if (this.clickedMenu === index) {
    this.clickedMenu = null;
  } else {
   this.clickedMenu = index;
  }
}

Let me know if it works for you or not. Thanks!

Working demo in this link Stackblitz

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply