I have a problem catching my object data in jQuery methods:
this.start = function() {
var prepend = '<img id = "' + this.imageList[0].name + '" class = "desktop_image_slider_prepended_image" src = "res/img/' + this.imageList[0].name + '.png"></img>';
$("#desktop_image_slider").prepend(prepend);
this.boxList.shift(this.imageList[0]);
$("#" + this.boxList[this.boxList.length-1].name).animate({"width":"0%"}, 500);
$("#" + this.imageList[0].name).animate({"width":"11.1111111111%"}, 500, function() {
$("#" + this.boxList[this.boxList.length-1].name).remove();
this.imageList.push(this.boxList[this.boxList.length-1]);
this.boxList.splice(this.boxList.length-1, 1);
$timeout(function() {
this.start();
}, 1000);
});
}
It throws undefined. It is obviously taking something from jQuery not from my method. Is there a way to access my method?
Thanks!
thisreferenced in your animate function event handler is referencing the calling event function. Not your globalthis. You can either use.bind(this)or useconst _self = this;and then reference_self.imageList.