我只是尝试从typescript中的数组中删除对象,在angular 2.4.0中,让我显示代码,它是我的html文件:

button type="submit" (click)="addAnotherLanguague()" >Add non native languague</button>
<li *ngFor="let languague of listOfLanguagues;">
     <div class="form-item form-item--text">
           <label class="label invisible">Years studied</label>
           <input type="number" min="0" [(ngModel)]="languague.yearsStudied" name="years"  placeholder="Years studied"/>
     </div>
<button type="submit" (click)="removeLanguague(languague)" >Remove</button> // here you can see use of method
</li>

并且有component.ts
(...)
this.listOfLanguagues = new Array <LanguagueInformationData>();
    }
addAnotherLanguague(){
        this.listOfLanguagues.push(new LanguagueInformationData);
    }
    removeLanguague(languague){
        this.listOfLanguagues.slice(this.listOfLanguagues.indexOf(languague), 1);
    }
(...)

添加效果很好,但是我尝试了所有删除操作,但仍然不知道如何传输该语言的引用,我不想使用.pop,因为我想完全删除该语言(在按钮下面)。
你能帮助我吗?

[编辑]
我再次遇到此代码的问题,因为每次我尝试添加新的languague(push)都会清除数组中存在的类上的数据,您知道会导致什么吗?

最佳答案

<li *ngFor="let languague of listOfLanguagues; let i = index"><button type="submit" (click)="removeLanguague(languague, i)" >Remove</button>

removeLanguague(languague, index){
    this.listOfLanguagues.splice(index, 1);
}

10-07 20:36