Javascript 为什么不改变数组?
有一个服务使用Javascript 为什么不改变数组?,javascript,angular,typescript,Javascript,Angular,Typescript,有一个服务使用set/get方法管理列表: @Injectable() export class ListService { private items: any[] = []; getItems() { return this.items; } setItems<T>(items: T[]): T[] { this.items = [...items]; return this.items;
set/get
方法管理列表:
@Injectable()
export class ListService {
private items: any[] = [];
getItems() {
return this.items;
}
setItems<T>(items: T[]): T[] {
this.items = [...items];
return this.items;
}
setSelected(value: any): void {
value.selected = !value.selected;
}
setSelectedState(value: any, state: boolean): void {
value.selected = state;
}
drop(event: CdkDragDrop<string[]>) {
moveItemInArray(this.items, event.previousIndex, event.currentIndex);
}
emitChnages(): void {
this.onChanged.next(this.items);
}
}
为什么我调用this.listService.drop(事件)
它不会改变数组这个。copysogllist
?你可以
you can just make
drop(event: CdkDragDrop<string[]>) {
moveItemInArray(this.items, event.previousIndex, event.currentIndex);
return this.items
}
and in your component
public drop(event: CdkDragDrop<string[]>): void {
this.copysogllist = this.listService.drop(event);
}
drop(事件:CdkDragDrop){
moveItemInArray(this.items、event.previousIndex、event.currentIndex);
将此邮件退回
}
在你的组件中
公共投递(事件:CdkDragDrop):无效{
this.copysoglist=this.listService.drop(事件);
}
您在哪里订阅了observable?什么?我不使用订阅我传递数组setItems(items:T[]):T[]{}
然后在服务中调用drop方法移动数组的元素您正在复制数组。为什么您希望原始数组发生更改?我复制了,但随后将复制的数组返回到componen的变量this.copysogllist
moveItemInArray()do做什么?顺便说一句,如果您使用此变量,另一个无法处理输入的页面,您应该会看到ngrx。这是解决这类问题的完美办法
you can just make
drop(event: CdkDragDrop<string[]>) {
moveItemInArray(this.items, event.previousIndex, event.currentIndex);
return this.items
}
and in your component
public drop(event: CdkDragDrop<string[]>): void {
this.copysogllist = this.listService.drop(event);
}