我只是尝试在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,因为我想删除正好在下面的这个按钮下面的语言。 你能帮我吗?
[编辑] 我再次遇到了这个代码问题,因为每次尝试添加新语言(push)时它会清除数组中现有类的数据,你知道是什么原因吗?