There are times in Typescript when 'this' gets a little ambiguous. Often, what I can do is call one function from another while binding 'this', e.g.
this.myFunction.bind(this);
That way, when myFunction() calls this.whatever()
, it knows what 'this' is.
In this particular case however, I'm a little lost. I am combining JQuery with Angular which, honestly, is never recommended, but sometimes you gotta work with what you're given. Anyway, the code below that is calling the function named "getSlice()" is inside of a JQuery loop which is being called from a ngAfterViewInit() event function. How do I properly call getSlice() so that the code can find it?
getSlice(val): TimeSlice {
const data = val.split(' ');
const slice = new TimeSlice(val[0], val[1], val[2]);
return slice;
}
ngAfterViewInit(): void {
let current: TimeSlice = new TimeSlice();
$('#selectable').selectable({
stop: function() {
const result = $('#select-result').empty();
$('li.ui-selected', this).each(function() {
const val = $(this).attr('value');
current = this.getSlice(val); <!-- Here is the problem child -->
});
}
});
}