2 回答

TA貢獻1831條經驗 獲得超4個贊
this由于函數作用域的原因,將失去其引用。您可以使用箭頭函數,這樣它就可以保留其范圍,甚至可以將該isEqualToSelectedText函數移出該類中的私有方法。
它可能是這樣的:
// in the same place you're declaring the function
const isEqualToSelectedText = (element : History) => {
return element.basic_text.basic_text === this.chosedText;
}
// or moving out to a private method in the class
export class DetailsComponent implements OnInit {
history: History[] = [
{
basic_text: { basic_text : 'text_1' },
summary: { summary : 'summary_1' },
tager: { tag_algorytm : true, tag_text: 'tag_1' }
},
{
basic_text: { basic_text : 'text_2' },
summary: { summary : 'summary_2' },
tager: { tag_algorytm : false, tag_text: 'tag_2' }
}
];
chosedText: string = 'text_1';
textFilter(h: History[]) {
let result: History[] = [];
var param = this.chosedText;
var foundIndex= h.findIndex(this.isEqualToSelectedText, param); // <-- changing here
result.push(h[foundIndex])
return result;
}
// The function was moved out
private isEqualToSelectedText(element: History) {
return element.basic_text.basic_text === this.chosedText;
}
}
添加回答
舉報