angularangular-materialangular2-material

angular2 material access tab values


I have the following code:

<md-tab-group (selectChange)="doSomething()">
  <md-tab *ngFor="let p of something))">
    <template md-tab-label>
     {{p.name}} ...
    </template>
   </md-tab>
 </md-tab-group>

Now doSomething() get's fired on Tab-Change and I can access the selected Index, but is there any way to access values of my *ngFor loop? I'd need 'p.id' in my code to fetch data from my database, depending on the open tab. Or is there a much more elegant solution I am not thinking of? Thanks for any help :)


Solution

  • For anyone interested, I solved my problem this way:

    1. Add an data-attrib to the tab generated by *ngFor and pass the whole tabgroup to my function:
    <md-tab-group #tabgroup (selectChange)="doSomething(tabgroup)">
              <md-tab *ngFor="let p of something" [attr.data-pid]="p.id">
                <template md-tab-label>
                 {{p.name}} ...
                </template>
               </md-tab>
             </md-tab-group>
    
    1. Get my selected md-tab, with it my nativeElement, and from that get my data-attrib. Not nice & clean, but it works.
    doSomething(tabgroup: MdTabGroup) {
    
            let pid = tabgroup._tabs.find((e, i, a) => i == tabgroup.selectedIndex)
                .content.viewContainerRef.element.nativeElement.dataset.pid;
    
            console.log(pid);
        }