I have an angular2 app where the component class is getting a little to big. I've decided to create a folder with 'Handler' classes, so i could put the related code in each handle class. In my component class i then create an instance of each handler and use these in the template
My component:
export class DesignerComponent {
public tab: TabHandler = new TabHandler(this);
My handler:
export class TabHandler {
public designerComponent: DesignerComponent;
constructor(designerComponent: DesignerComponent)
{
this.designerComponent = designerComponent
}
add() {
if (this.designerComponent.formModel.tabs.length < 1) {
this.designerComponent.formModel.tabs.push(new TabModel("Tab"));
}
this.designerComponent.formModel.tabs.push(new TabModel("Tab"));
}
My template:
<li *ngFor="let tab of formModel.tabs; let i = index; ">
<a href="#" [ngClass]="{'tab-selected': formModel.currentTab == tab}"
class="tab-button"
attr.contenteditable="{{formModel.currentTab == tab}}"
(blur)="tab.changeShortTitle($event)"
(dragover)="allowDrop($event,'tab')"
(dragstart)="drag($event,i,'tab')"
(drop)="drop($event,i)"
(dragleave)="dragleave()"
(click)="tab.select(tab)">
{{tab.shortTitle}}
</a>
</li>
<li id="add-tab-buttonholder">
<a href="#" (click)="tab.add()"><i class="fa fa-plus" aria-hidden="true"></i></a>
</li>
the last <li id="add-tab-buttonholder">
<a href="#" (click)="tab.add()
works because it's outside of the *ngFor loop.
I have tried to create an empty function in the class, and call it in the lopp, but that also fails with the same error message
ORIGINAL EXCEPTION: TypeError: self.context.$implicit.select is not a function
add(), you should calltab.add(). But without seeing your template, this is hard to answer