Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 在何处存储选定的元素列表?_Angular_Typescript_Angular8 - Fatal编程技术网

Angular 在何处存储选定的元素列表?

Angular 在何处存储选定的元素列表?,angular,typescript,angular8,Angular,Typescript,Angular8,我有一些元素列表: let list1 = [1,2,3,4]; let list2 = [1,2,3,4]; let list3 = [1,2,3,4]; 我在循环中互相迭代: <div *ngFor="let el of list1" (click)="set(el)">{{el}</div> <div *ngFor="let el of list2" (click)="set(el)">{{el}</div> <div *ngF

我有一些元素列表:

let list1 = [1,2,3,4]; 
let list2 = [1,2,3,4]; 
let list3 = [1,2,3,4]; 
我在循环中互相迭代:

<div *ngFor="let el of list1" (click)="set(el)">{{el}</div>
<div *ngFor="let el of list2" (click)="set(el)">{{el}</div>
<div *ngFor="let el of list3" (click)="set(el)">{{el}</div>
怎么做?我是否应该创建一个三个模型类:

class FilterSetter {
   set();
   unset();
}

class FilterList1 extends FilterSetter {
    selected = [];
}

class FilterList2 extends FilterSetter {
   selected = [];
}

class FilterList3 extends FilterSetter {
    selected = [];
}
我不喜欢我的解决方案,因为它迫使我创建了很多分类的if列表,这些列表将在将来添加


此外,我还需要将json与所有选定元素一起输出。好吧,在
集合中添加另一个参数,类似于source,以了解数据来自哪个源

编辑:尽量不要使用
set
作为名称,使用
addSelectedElem

mySelectedElems: {
    list1: [];
    list2: [];
    list3: [];
}; // if you know already the names of the list. idk

addSelectedElem(elem, source) {
    mySelectedElems[source] = mySelectedElems[source] || []
    mySelectedElems[source].push(elem)
}

那么,这个代码应该放在哪里呢?我不会弄乱组件,因为它应该只显示数据。selectedFilters它应该存储在哪里?在service Angular中,我尝试将所有责任转移到service。传递集合名称有点麻烦
mySelectedElems: {
    list1: [];
    list2: [];
    list3: [];
}; // if you know already the names of the list. idk

addSelectedElem(elem, source) {
    mySelectedElems[source] = mySelectedElems[source] || []
    mySelectedElems[source].push(elem)
}