How to call a function on column.render() ?

How to call a function on column.render() ?

ShaNicShaNic Posts: 5Questions: 2Answers: 0

Hi,

I'm using Angular datatables for my angular app and i want to call a function on one of my column.render() to have something like :

columns: [{
        title: 'My Column name',
        data: 'id',
render (data){
return this.usersService.getUserName(data);
}}     }],

usersService is initate on my constructor() but when i trying to do that, i cant call usersService.

I have this error :
ERROR TypeError: Cannot read property 'usersService' of undefined

My dtOption is on ngOnInit().

If someone can help me..

Thanks,
Nick

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 64,083Questions: 1Answers: 10,567 Site admin
    Answer ✓

    The execution scope is wrong - try:

    render: (data) => {
      return this.usersService.getUserName(data);
    }
    

    I believe that the short hand you are using expands to:

    render: function (data) { ... }
    

    hence why this changes. You want a fat arrow function to execute in the same scope as your parent function.

    Allan

  • ShaNicShaNic Posts: 5Questions: 2Answers: 0

    Thank you ! It works !
    Nick

This discussion has been closed.