0

This is my code :

    export class CustomToast implements OnInit {
            toastTypeGroup: string;
            message: string;
            title: string; 
            showDuration= "300";

            constructor(_toastTypeGroup: string, _message:string, _title: string ){
                this.toastTypeGroup = _toastTypeGroup;
                this.message = _message;
                this.title =  _title;
            }

            ngOnInit() {
                **//this is the solution** 
                **var _this = this;**
                console.log(this.showDuration);
                var ToastrDemo = function () {
                    var toastr: any = (<any>window).toastr;
                    var k, m = -1, f = 0;

                    var t = function () {
                        var t, o,
                            //toastTypeGroup
                            e = this.toastTypeGroup,
                            //message
                            n = this.message,
                            //title
                            a = this.title,
                            //showDuration
                            i = this.showDuration,
                    };

                    return { init: function () { t() } }
                }();jQuery(document).ready(function () { ToastrDemo.init() });

            }



        }

My problem is that I want to read members values from my class called CustomToast inside my JavaScript function called var t = function () {...}.

I can't get typescript class member value inside a JavaScript Function. e.g "toastTypeGroup" member is not accessible iside my fucntion This is the big problem.

Thanks in advance.

antony
  • 27
  • 1
  • 1
  • 8
  • You can try with an arrow callbacks: `let ToastrDemo = () => { ... let t = () => { ... } };`, `return { init: () => { t() } }` and `jQuery(document).ready(() => { ToastrDemo.init() });`. In short, replace every occurrence of `function()` with `() =>`. – ConnorsFan May 08 '18 at 13:10
  • Possible duplicate of [How to access the correct \`this\` inside a callback?](https://stackoverflow.com/questions/20279484/how-to-access-the-correct-this-inside-a-callback) – ConnorsFan May 08 '18 at 13:13
  • thanks guys. I just moved this line " var _this = this; " before to call ToastrDemo function. and I already update my post to the right behaviour. – antony May 08 '18 at 15:30

3 Answers3

1

Move

var _this = this;

to the beginning of ngOnInit and use _this.showDuration or use bind(this)

justMe
  • 674
  • 7
  • 26
  • 1
    thanks guys. I just moved this line " var _this = this; " before to call ToastrDemo function. and I already update my post to the right behaviour. – antony May 08 '18 at 15:31
0

Not sure I understood the question but wouldn't you just add .bind(this) to your definition of t?

var t = function () {
    var t, o,
        //toastTypeGroup
        e = this.toastTypeGroup,
        //message
        n = this.message,
        //title
        a = this.title,
        //showDuration
        i = this.showDuration,
}.bind(this);
ed'
  • 1,815
  • 16
  • 30
  • not working! may be because T fucntion it's inside var ToastrDemo = function () {..} what do u think ? – antony May 08 '18 at 12:36
  • thanks guys. I just moved this line " var _this = this; " before to call ToastrDemo function. and I already update my post to the right behaviour. – antony May 08 '18 at 15:31
0

try

/** @type {your type} */
var _this = this;

https://www.typescriptlang.org/docs/handbook/type-checking-javascript-files.html

bluelovers
  • 1,035
  • 2
  • 10
  • 22
  • thanks guys. I just moved this line " var _this = this; " before to call ToastrDemo function. and I already update my post to the right behaviour. – antony May 08 '18 at 15:31