Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/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
Javascript 为什么不改变数组?_Javascript_Angular_Typescript - Fatal编程技术网

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);
        
    }