2

I would like to change the isMyPresentationEmpty variable when the stop event of JQuery sortable fires. Unfortunately the 'this' variable is undefined in that scope... Do you know how I am able to access the variable from that scope?

Thank you so much in advance!

import { Component, OnInit, ElementRef, Inject } from 'angular2/core';

declare var jQuery: any;

@Component({
    selector: 'edit-presentation',
    templateUrl: 'app/Edit Presentation/edit_presentation.component.html'
})
export class EditPresentationComponent implements OnInit {
    elementRef: ElementRef;

    isMyPresentationEmpty = true;

    constructor(@Inject(ElementRef) elementRef: ElementRef) {
        this.elementRef = elementRef;
    }

    ngOnInit() {

        // Initialize Sortable on lists
        var oldList, newList, item;
        jQuery(this.elementRef.nativeElement).find('.sortable-list').sortable({
            start: function(event, ui) {
                item = ui.item;
                newList = oldList = ui.item.parent().parent();
            },
            change: function(event, ui) {  
                if (ui.sender) newList = ui.placeholder.parent().parent();

            },
            stop: function(event, ui) {  
                // Check for empty list and hide/show instruction text
                if (jQuery(event.target).has('li').length) {
                    this.isMyPresentationEmpty = false;
                } else {
                    this.isMyPresentationEmpty = true;
                }
            },
            connectWith: ".sortable-list"
        }).disableSelection();
    }

}
1
  • 1
    jQuery inside Angular2 ?!? :( Commented Apr 25, 2016 at 12:22

2 Answers 2

5

You can bind "this" to a variable (var self in this example) outside your events and then use that variable inside the events. For example:

ngOnInit() {
        var self = this;  //use self instead of this if you want to refer to your component inside events
        // Initialize Sortable on lists
        var oldList, newList, item;
        jQuery(this.elementRef.nativeElement).find('.sortable-list').sortable({
            start: function(event, ui) {
                item = ui.item;
                newList = oldList = ui.item.parent().parent();
            },
            change: function(event, ui) {  
                if (ui.sender) newList = ui.placeholder.parent().parent();

            },
            stop: function(event, ui) {  
                // Check for empty list and hide/show instruction text
                if (jQuery(event.target).has('li').length) {
                    self.isMyPresentationEmpty = false;
                } else {
                    self.isMyPresentationEmpty = true;
                }
            },
            connectWith: ".sortable-list"
        }).disableSelection();
    }
Sign up to request clarification or add additional context in comments.

5 Comments

This is the JS way... You can just use arrow-functions in typescript that do not change the this context in a function.
@Dinistro Yes but then you have to do other adjustments for example if you want to select $(this) = $(event.target). That may have been a case so I stayed on the safe side.
Oh, of course. I was not thinking about this. This is perhaps the reason, why using JQuery in angular2 is not that nice.
Indeed. I agree :)
If I could have avoided JQuery I would have. But I could not find any Angular2 solution for making my lists sortable...
1

You can use the arrow-functions from typescript, the do not change the this.

import { Component, OnInit, ElementRef, Inject } from 'angular2/core';

declare var jQuery: any;

@Component({
    selector: 'edit-presentation',
    templateUrl: 'app/Edit Presentation/edit_presentation.component.html'
})
export class EditPresentationComponent implements OnInit {
    elementRef: ElementRef;

    isMyPresentationEmpty = true;

    constructor(@Inject(ElementRef) elementRef: ElementRef) {
        this.elementRef = elementRef;
    }

    ngOnInit() {

        // Initialize Sortable on lists
        var oldList, newList, item;
        jQuery(this.elementRef.nativeElement).find('.sortable-list').sortable({
            start: (event, ui) => {
                item = ui.item;
                newList = oldList = ui.item.parent().parent();
            },
            change: (event, ui) => {
                if (ui.sender) newList = ui.placeholder.parent().parent();

            },
            stop: (event, ui) => {
                // Check for empty list and hide/show instruction text
                if (jQuery(event.target).has('li').length) {
                    this.isMyPresentationEmpty = false;
                } else {
                    this.isMyPresentationEmpty = true;
                }
            },
            connectWith: ".sortable-list"
        }).disableSelection();
    }

}

Setting the this into a variable is a JavaScript solution and not the typescript way.

I hope this helped you.

Comments

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.