plauzul / ng2-izitoast

MIT License
19 stars 8 forks source link

Cant Call Service/Function Angular Way in onClosing/OnClosed Question #9

Closed dreamid27 closed 6 years ago

dreamid27 commented 6 years ago

Hi plauzul, thank for your awesome plugin, that helps me. but I have some problem. when I try to make this question toast.

I'm trying in OnClosing event and call my service/my function, but it does not work! the error say Cannot read property 'roleService' of undefined. and I think it's happen in any event. cause the TypeScript/Angular have different habits.

This is my code, sorry for my bad English,

image

Albejr commented 6 years ago
  1. Use Arrow Function Expression in buttons
  2. Alter order of paramters of hide method
  3. Explicitly declare that so you have a reference for that.roleService
deleteRole(objModel: RoleModel)
{
    var message = `Apakah abda yakin akan manghapus ${objModel.name} ?`;
    var that = this;

    this.iziToast.question({
    ...
        buttons: [
            ['<button><b>YA</b></button>', (instance, toast) => {
                instance.hide(toast, { transitionOut: 'fadeOut' }, 'button');
                this.roleService.getRoles();
            }, true],
            ['<button>NO</button>', (instance, toast) => {
                instance.hide(toast, { transitionOut: 'fadeOut' }, 'button');
            }]
        ],
        onClosing: () => {
            that.roleService.getRoles();
        }
    });
dreamid27 commented 6 years ago

@Albejr Ahhhh, Thank bro, it's solved, you really - really help me.

I'm sorry for ask this stupid question, I just started being a programmer.

but, can I ask some question? as far as i know, change the code 'function () ' to ' () =>' it's just change es5 to es6. and actually, it is the same.

what actually makes it different?

Albejr commented 6 years ago

In this case, the "this" scope of a "function () {}" is only internal to the function itself.

With "arrow function" it can be either the scope of a function or the global scope.

dreamid27 commented 6 years ago

ahh i see, thank very much for explaining that.

I will close it