Angular 基于@Input修改变量

Angular 基于@Input修改变量,angular,Angular,我在一个组件中有一个数组,它在初始化期间被初始化。根据@input值,需要修改数组。在onChanges期间,数组似乎是空的。如何做到这一点 export class SomeComponent implements OnInit, OnChanges { @Input() pickedStudents: Student[] = []; studentSubscription: Subscription; students: Student[] = []; constructor(private

我在一个组件中有一个数组,它在初始化期间被初始化。根据@input值,需要修改数组。在onChanges期间,数组似乎是空的。如何做到这一点

export class SomeComponent implements OnInit, OnChanges {
@Input()
pickedStudents: Student[] = [];
studentSubscription: Subscription;
students: Student[] = [];
constructor(private studentService: StudentService) {
}
ngOnInit() {
    this.studentSubscription = this.studentService.getStudents().subscribe(a => this.students = a);
}
ngOnChanges() {
    if (this.pickedStudents && this.pickedStudents.length) {
        //This is for instance. The problem is students seem to be empty here.
        this.students.splice(this.students.indexOf(this.pickedStudents[0]), 1);
    }
}
ngOnDestroy() {
    this.studentSubscription.unsubscribe();
}
}

只需在ngOnChanges中设置一个条件,如下所示

ngOnChanges(){
    if(this.arrayName && this.arrayName.length){
            your operation
    }
}

没有人知道如果你不显示你的代码会发生什么…这是一个子组件还是
pickedStudents
来自哪里?