Run angular function within jQuery

0

I'm doing an application in angular, I want to make that when the user's scroll is up automatically download more posts (that is done by pressing a button, but not automatically when your scroll is down). With jquery I make it execute something when the scroll is down, but I can not get it to execute my function of the angular component, here my code

Note that when I call the function / method from outside of angle if it recognizes it, but from the inside it does not recognize it as any type and when executing in the browser in the angle compiler it tells me that there is no loadMas function ( )

    
asked by Javi Tellez 22.10.2018 в 22:07
source

1 answer

0

That's because the this within the function points to the jQuery object, because you use

.. function() { ... }

const BOTON = $('button');

BOTON.click( function(e) {

  console.log( this === window ); // false
  console.log( this === e.currentTarget ); // true
  console.log( this === BOTON.get(0) ); // true
   
});

BOTON.click( e => {

  console.log( this === window ); // true
  console.log( this === e.currentTarget ); // false
  console.log( this === BOTON.get(0) ); // false
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button>Ver donde apuntan los this</button>

More info on Functions Arrow

Simple solutions:

  • Use ()=> (Arrow Functions)
  • Assign a variable outside the jQuery function:
  • let _this = this;

    and use it within the jQuery function:

    _this.cargarMas();

        
    answered by 22.10.2018 / 22:20
    source