简体   繁体   中英

How can I call a function inside a class from inside a function called up by Ajax in typescript?

I have the following typescript code:

   export class Modal {

        private link: Link;

        constructor (public $link: JQuery) {
            this.link = new Link($link);
            this.ajaxGet(this.link);
        }

        ajaxGet(link: Link) {
             $.ajax(link.Href,
            {
                context: {
                    link: link
                },
                dataType: 'html'
            })
                .done(this.ajaxDone)
                .fail(this.ajaxFail);
        }

        ajaxDone(data: string, textStatus: string, jqXHR: JQueryXHR) {
            var link = <Link> this.link;
            link.Modal.Content = data;
            this.create(link);
        }

        create(link: Link) {
           var a = link;
        }


    }

This works up to the point where in the ajaxDone I have a this.create(link). The problem is that the "this" is no longer the Modal and it has no create function. How can I get it to call the Modal.Create() function at this point?

I think your context is wrong in your Ajax call - the context is...

This object will be made the context of all Ajax-related callbacks

http://api.jquery.com/jQuery.ajax/

$.ajax(this.link.Href, {
    context: this,
    dataType: 'html'
})
.done(this.ajaxDone)
.fail(this.ajaxFail);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM