Jump to content
Search Community

Angular2 and greensock "this" reserved word

Keaton88 test
Moderator Tag

Go to solution Solved by Keaton88,

Warning: Please note

This thread was started before GSAP 3 was released. Some information, especially the syntax, may be out of date for GSAP 3. Please see the GSAP 3 migration guide and release notes for more information about how to update the code to GSAP 3's syntax. 

Recommended Posts

Hi

I'm building and angular 2 app that uses greenaock's draggable features. Both angular 2 and greensock utilize the reserved word "this" to call functions. When the onPress function is called on an Draggable item, I'd like to call an angular 2 function which is done by using "this.functionName()", however I am not able to because "this" is currently pointing to greensock's object. Is there a work around for this? Thank you

Link to comment
Share on other sites

  • Solution

I solved the problem.  Prior to calling the Draggable.create function, you reference the "this" keyword in a local variable like self.

For example

initDraggableItem_Characters() {
    let self = this; // references the angular 2 "this"
    Draggable.create('.itemDrag', {
      edgeResistance: 1,
      autoScroll: 1,
      liveSnap: false,
      throwProps: true,
      onPress: function () {
        this.activeCharacterType = self.getActiveCharacterType(this.target.id); // angular 2 function call
        this.startX = this.x; // greensock's this
        this.startY = this.y; // greensock's this
        .......

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...