我正在寻找一种方法在将 select 选项推送到 angular 2+ 中的数组后将其删除
I'm looking for a way to remove select option after its pushed to array in angular 2+
我需要一种方法在将元素添加到数组后从 select 列表中删除元素,不允许再次插入它,这是下拉列表:
<div class="modal-body">
<select class="form-control select2-hidden-accessible" id="ddlGrupos" name="user Selecionado" [(ngModel)]="userSelected" (change)="selectValorUsuario(userSelected)">
<option *ngFor="let usuario of Usuario" [ngValue]="usuario">{{usuario.nome}}</option>
</select>
</div>
这是推送后必须从列表中删除的推送方法:
adicionarArrUsuario() {
const result = this.unidadeUsuarioArr.some(t => t.id === this.usuarioObjectSelect.id);
if (!result) {
this.unidadeUsuarioArr.push({
id: this.usuarioObjectSelect.id,
nome: this.usuarioObjectSelect.nome
});
}
}
您可以使用 Array.filter:
选择后删除元素
selectValorUsuario(userSelected) {
this.Usuario = this.Usuario.filter(usuario => usuario.id !== userSelected.id);
}
将此方法应用于您的逻辑。
我需要一种方法在将元素添加到数组后从 select 列表中删除元素,不允许再次插入它,这是下拉列表:
<div class="modal-body">
<select class="form-control select2-hidden-accessible" id="ddlGrupos" name="user Selecionado" [(ngModel)]="userSelected" (change)="selectValorUsuario(userSelected)">
<option *ngFor="let usuario of Usuario" [ngValue]="usuario">{{usuario.nome}}</option>
</select>
</div>
这是推送后必须从列表中删除的推送方法:
adicionarArrUsuario() {
const result = this.unidadeUsuarioArr.some(t => t.id === this.usuarioObjectSelect.id);
if (!result) {
this.unidadeUsuarioArr.push({
id: this.usuarioObjectSelect.id,
nome: this.usuarioObjectSelect.nome
});
}
}
您可以使用 Array.filter:
选择后删除元素selectValorUsuario(userSelected) {
this.Usuario = this.Usuario.filter(usuario => usuario.id !== userSelected.id);
}
将此方法应用于您的逻辑。