Javascript 如何使过滤器列表适用于对象中的对象?它适用于其他数据类型
我很难理解为什么下面的代码不起作用Javascript 如何使过滤器列表适用于对象中的对象?它适用于其他数据类型,javascript,angular,typescript,ionic-framework,filter,Javascript,Angular,Typescript,Ionic Framework,Filter,我很难理解为什么下面的代码不起作用 initializeItems(): void { this.notesService.load();} this.notesService.load()创建一个包含多个对象的对象,例如notes,该对象具有多个属性,例如notes.title过滤器列表(evt){}。当我使用列表时,代码可以工作,但对我的对象不起作用 filterList(evt) { this.initializeItems(); const search
initializeItems(): void {
this.notesService.load();}
this.notesService.load()
创建一个包含多个对象的对象,例如notes,该对象具有多个属性,例如notes.title<当在搜索栏中注册输入时,会触发代码>过滤器列表(evt){}。当我使用列表时,代码可以工作,但对我的对象不起作用
filterList(evt) {
this.initializeItems();
const searchTerm = evt.srcElement.value;
if (!searchTerm) {
return;
}
this.notesService.notes = this.notesService.notes.filter(currentGoal => {
console.log(this.notesService.notes)
if (currentGoal.title && searchTerm) {
if (currentGoal.title.toLowerCase().indexOf(searchTerm.toLowerCase()) > -1) {
return true;
}
return false;
}
});
}
如果我删除这个.initializeItems()
;从filterList(evt)
中,我可以看到过滤工作正常。但是,我希望包括this.initializeItems()
,以便当用户使用退格时,忽略先前的筛选,并再次显示所有对象。当我不包括this.initializeItems()
时,我可以看到this.notesService.notes
在过滤后为空
当我包含this.initializeItems()
时,不会发生过滤。有人能告诉我为什么这不起作用吗?例如,当我使用包含数据的列表时,这些函数可以正常工作
阐明initializeItems()在Notes包含id、标题、内容和日期时的作用
export class NotesService {
public notes: Note[] = [];
public loaded: boolean = false;
constructor(private storage: Storage,private navCtrl: NavController) { }
load(): Promise<boolean> {
return new Promise((resolve) => {
this.storage.get('notes').then((notes) => {
if(notes != null){
this.notes = notes;
}
this.loaded = true;
resolve(true);
导出类注释服务{
公开注释:注释[]=[];
公共加载:布尔值=false;
构造函数(专用存储:存储,专用navCtrl:NavController){}
load():承诺{
返回新承诺((解决)=>{
this.storage.get('notes')。然后((notes)=>{
如果(注意!=null){
this.notes=注释;
}
this.loaded=true;
决心(正确);
如果不了解更多有关初始化项的信息,我想回答这个问题是不可能的。这个函数中有很多状态(this.notesService.notes
,currentGoal.title
,等等)我们只是看不到。谢谢,我的更新更清楚了吗?@johmmitth load是异步的,你不能期望在调用函数时完成。你必须等待承诺解决,才能继续你的代码谢谢!我该怎么做?