Javascript 在jQuery函数中调用函数
我想在JQuery方法中执行一个exeenal函数。当我尝试调用该方法时出现问题,该方法看起来未定义。我怎样才能解决这个问题?我使用带有角度2的Typescript进行ambJavascript 在jQuery函数中调用函数,javascript,function,angular,Javascript,Function,Angular,我想在JQuery方法中执行一个exeenal函数。当我尝试调用该方法时出现问题,该方法看起来未定义。我怎样才能解决这个问题?我使用带有角度2的Typescript进行amb ngAfterViewInit() { jQuery(".mo-table").scroll(function () { var trueDiveHeight = jQuery(".mo-table")[0].scrollHeight; var divHeight = jQuery(".mo-table").he
ngAfterViewInit() {
jQuery(".mo-table").scroll(function () {
var trueDiveHeight = jQuery(".mo-table")[0].scrollHeight;
var divHeight = jQuery(".mo-table").height();
var scrollLeft = trueDiveHeight - divHeight;
if (jQuery(".mo-table").scrollTop() >= scrollLeft - 150) {
this.onSearch();
console.log("new bottom")
}
});
}
onSearch方法是一个外部函数,未定义
onSearch(): void {
this.updateTableReport(this.scrollId, this.buildParams())
}
任何帮助都将不胜感激 变化
jQuery(".mo-table").scroll(function () {
到
您的此
不是指您的组件
还是老办法:
ngAfterViewInit() {
var self = this; //<-- assign this to self here
jQuery(".mo-table").scroll(function () {
var trueDiveHeight = jQuery(".mo-table")[0].scrollHeight;
var divHeight = jQuery(".mo-table").height();
var scrollLeft = trueDiveHeight - divHeight;
if (jQuery(".mo-table").scrollTop() >= scrollLeft - 150) {
self.onSearch(); //<-- use self here
console.log("new bottom")
}
});
}
ngAfterViewInit(){
var self=this;//=scrollLeft-150){
self.onSearch();//可能是重复的!谢谢,我不知道“self”逻辑。这是为什么?我的意思是,当我们分配给它时,是否获得整个页面的引用?@XGuy当你在javascript中使用work函数时,你正在创建一个新的作用域。这个作用域中的this
引用了这个函数的let say实例。因此this
将引用和中的不同内容退出此功能。有关的详细信息:
ngAfterViewInit() {
var self = this; //<-- assign this to self here
jQuery(".mo-table").scroll(function () {
var trueDiveHeight = jQuery(".mo-table")[0].scrollHeight;
var divHeight = jQuery(".mo-table").height();
var scrollLeft = trueDiveHeight - divHeight;
if (jQuery(".mo-table").scrollTop() >= scrollLeft - 150) {
self.onSearch(); //<-- use self here
console.log("new bottom")
}
});
}