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;
}
}
添加回答
举报