0

I'm making a custom component to build a table.

export class TableComponent implements OnChanges {
    @Input() records: any[];
    @Input() caption: string;
    @Input() config: TableConfig[];
}

export class TableConfig {
    basePath: string;
    actions?: object[];
}

//Actions example
/*

action = { icon: "", description: "", onClick: thisMustReceiveAFunction }

*/

I just call my component where I want the table

<my-table
     [records]="entities"
     [caption]="Table"
     [config]="myConfig">
</my-table>

And initialize my config inside my component caller

myConfig: TableConfig = {
    basePath: "users",
    actions: [
        {
            icon: "glyphicon glyphicon-pencil",
            description: "edit",
            onClick: this.MyEditFunction
        },
        {
            icon: "glyphicon glyphicon-trash",
            description: "delete"
            onClick: this.MyDeleteFunction
        }
    ]
};

MyEditFunction(): void { console.log('edit clicked'); }
MyDeleteFunction(): void { console.log('delete clicked'); } 

But when I try to make a call at my table template, it doesnt work.

<button class="btn btn-default" *ngFor="let action of config.actions" (click)="action.onClick()">
    <span class="{{action.icon}}"></span>
</button>
1
  • you get any error?? if so what is it? Commented Jun 9, 2017 at 17:40

1 Answer 1

1

You should be using type safety operator ? because the DOM renders even before your data is retrieved.

<button class="btn btn-default" *ngFor="let action of config?.actions" 
     (click)="action.onClick()">
    <span class="{{action?.icon}}"></span>
</button>

or you can use an *ngIf to ensure that the DOM is not loaded if the data is not available as below,

<span *ngIf="config.actions.length >  0">
   <button class="btn btn-default" *ngFor="let action of config.actions" (click)="action.onClick()">
        <span class="{{action.icon}}"></span>
   </button>
 </span>
Sign up to request clarification or add additional context in comments.

1 Comment

I think that its not the problem but now I rewrite the code and its working...So I will accept your answer because of the ? trick :D

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.