无法访问 "findIndex" 函数内的 class 变量 - 已解决
Cannot access class varaible withing "findIndex" function - resolved
我无法通过 findIndex 方法访问我的 class 变量。
My whole class:
import { Component, Input, OnInit } from '@angular/core';
import { History } from '../model/history';
import { SharedService } from "../shared.service";
@Component({
selector: 'app-details',
templateUrl: './details.component.html',
styleUrls: ['./details.component.scss']
})
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" }
}
];
basic_text: String = "Tekst podstawowy";
summary: String = "Streszczenie";
rating: String = "Ocenione zdania";
chosedText: string;
asd: string = 'asdad';
constructor(private sharedService: SharedService) { }
ngOnInit() {
this.sharedService.sharedMessage.subscribe(chosedText => this.chosedText = chosedText)
}
textFilter(h: History[]) {
let result: History[] = [];
var param = this.chosedText;
var foundIndex = h.findIndex(this.isEqualToSelectedText, param);
result.push(h[foundIndex])
return result;
}
// The function was moved out
private isEqualToSelectedText(element: History) {
return element.basic_text.basic_text === this.chosedText;
}
}
我正在为 thisArg 分配值 chosedText。
我遇到的错误:
core.js:5967 ERROR TypeError: Cannot read property 'basic_text' of undefined
at DetailsComponent_tr_12_Template (details.component.html:11)
at executeTemplate (core.js:9274)
at refreshView (core.js:9143)
at refreshEmbeddedViews (core.js:10263)
at refreshView (core.js:9167)
at refreshComponent (core.js:10309)
at refreshChildComponents (core.js:8940)
at refreshView (core.js:9193)
at refreshComponent (core.js:10309)
at refreshChildComponents (core.js:8940)
但如果我这样做
var x= h.findIndex(isEqualToSelectedText, 'text_1');
一切正常。
我试图从 isEqualToSelectedText 函数访问我的 class varialb。但我总是遇到这个错误。
在 findIndex 的 运行 期间,单词“this”应该等于 'text_1'。
你能帮帮我吗?
this
会因为函数作用域而失去它的引用。您可以使用箭头函数,以便它可以保留其范围,甚至可以将 isEqualToSelectedText
函数移出到 class.
中的私有方法
可能是这样的:
// 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;
}
}
好的,我找到错误了。在“http get”初始化之前,我正在使用我的变量。
至此问题解决,感谢帮助
我无法通过 findIndex 方法访问我的 class 变量。
My whole class:
import { Component, Input, OnInit } from '@angular/core';
import { History } from '../model/history';
import { SharedService } from "../shared.service";
@Component({
selector: 'app-details',
templateUrl: './details.component.html',
styleUrls: ['./details.component.scss']
})
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" }
}
];
basic_text: String = "Tekst podstawowy";
summary: String = "Streszczenie";
rating: String = "Ocenione zdania";
chosedText: string;
asd: string = 'asdad';
constructor(private sharedService: SharedService) { }
ngOnInit() {
this.sharedService.sharedMessage.subscribe(chosedText => this.chosedText = chosedText)
}
textFilter(h: History[]) {
let result: History[] = [];
var param = this.chosedText;
var foundIndex = h.findIndex(this.isEqualToSelectedText, param);
result.push(h[foundIndex])
return result;
}
// The function was moved out
private isEqualToSelectedText(element: History) {
return element.basic_text.basic_text === this.chosedText;
}
}
我正在为 thisArg 分配值 chosedText。 我遇到的错误:
core.js:5967 ERROR TypeError: Cannot read property 'basic_text' of undefined
at DetailsComponent_tr_12_Template (details.component.html:11)
at executeTemplate (core.js:9274)
at refreshView (core.js:9143)
at refreshEmbeddedViews (core.js:10263)
at refreshView (core.js:9167)
at refreshComponent (core.js:10309)
at refreshChildComponents (core.js:8940)
at refreshView (core.js:9193)
at refreshComponent (core.js:10309)
at refreshChildComponents (core.js:8940)
但如果我这样做
var x= h.findIndex(isEqualToSelectedText, 'text_1');
一切正常。 我试图从 isEqualToSelectedText 函数访问我的 class varialb。但我总是遇到这个错误。 在 findIndex 的 运行 期间,单词“this”应该等于 'text_1'。 你能帮帮我吗?
this
会因为函数作用域而失去它的引用。您可以使用箭头函数,以便它可以保留其范围,甚至可以将 isEqualToSelectedText
函数移出到 class.
可能是这样的:
// 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;
}
}
好的,我找到错误了。在“http get”初始化之前,我正在使用我的变量。 至此问题解决,感谢帮助