0

我有一个数组中的项目列表说,

Design
Analysis
Testing
Development

谁的索引,

0,1,2,3

在这里,我使用 angular dragula 用于拖放列表元素。使用dragula,我正在改变数组的位置说,

Analysis
Design
Development
Testing

顺序正在改变,但索引位置仍然相同,

1,0,3,2

这里的索引位置不是按顺序排列的,尽管它看起来像旧的索引值,但它看起来像旧的索引值。而无论数组内部发生什么变化,我都需要具有相同的索引位置,这意味着0,1,2,... so on即使顺序发生变化,索引位置也应该从开始。

我用过的 Html 组件,

    <div class="card-content">
        <div dropDirective (dropEvent)="addDropItem($event)" class="droppable" [dropHighlight]="'highlight'">
            <div [dragula]="'second-bag'">
                <app-generic-drop-box *ngFor="let item of itemsDropped; index as i" (editBtn)='editBtn($event)' (deleteBtn)='deleteBtn($event)' [genericBox]='item' class="arrow_box dropItem temp" (click)='indexOfItem(i)'> </app-generic-drop-box>
            </div>
        </div>
    </div>

在 ts 文件中,

        import { Component, OnInit, Input, Output, EventEmitter, ViewChild, ViewContainerRef, OnDestroy } from '@angular/core';
        import { Router, ActivatedRoute } from '@angular/router';
        import { ScenarioMapService } from '../scenario-map.service';

        import { DynamicFormModel } from '../dynamic-form-config/dynamic-form-model';
        import { Textbox } from '../dynamic-form-config/textbox';
        import { CONFIG } from '../../app.config';

        @Component({
          selector: 'app-drop-area',
          templateUrl: './drop-area.component.html',
          styleUrls: ['./drop-area.component.scss']
        })
        export class DropAreaComponent implements OnInit, OnDestroy {

        indexOfItem(i) {
        console.log(i); 
        }

    }

这里console.log(i)显示了索引位置,如果用户单击列表,如果我更改数组的位置,我需要在更改后获取有序索引,如0,1,2,3.....请帮助我实现结果。

4

1 回答 1

0

要重置 AngularJS 数组中的索引,您可以完全过滤它而无需任何操作。

这样索引就会重新设置。

resetArr = orgArr.filter(function(){return true;});

数组索引1,0,3,20,1,2,3再次出现,但数据仍然存在。

作为参考:需要仅重置 JavaScript 数组 Stackoverflow 线程的索引...

于 2018-06-20T08:59:43.440 回答