简体   繁体   English

Angular 8:如何在 Html 顶部的数组中显示动态添加的 object?

[英]Angular 8: How to display dynamically added object in an array at top in Html?

I have a formArray that add/remove object dynamically from it.我有一个formArray ,可以从中动态添加/删除 object。 I have a *ngFor to display that formArray.我有一个*ngFor来显示该 formArray。 At the same time I have to patch the stored array to that formArray, Now if I want to add another object it pushes at last index and displays at last in Html view, I want to display the last added object at top, so how can i do that?同时我必须将存储的数组修补到该formArray,现在如果我想添加另一个object,它会在最后一个索引处推送并最后在Html视图中显示,我想在顶部显示最后添加的ZA8CFDE6331BD59EB66AC96F8911,所以可以如何我这样做? I just want to display.我只想展示。

here is the code ts这是代码ts

 this.form = this.fb.group({ project: this.fb.array([ this.fb.group({ type: ['', Validators.required], date: ['', Validators.required], amount: ['', Validators.required], }) ]) }) get projectArray() { return this.form.get('project') as FormArray; } addProjectArray() { this.projectArray.push( this.fb.group({ type: ['', Validators.required], date: ['', Validators.required], amount: ['', Validators.required], }) ); } deleteProject(index) { this.projectArray.removeAt(index); }

html

 <div class="" *ngFor="let item of projectArray.controls; let i=index" [formGroupName]="i"> <div class="row"> <div class="col-sm-3"> <div class="form-group"> <label class="">Type:</label> <select class="" formControlName="type"> <option value="1">1</option> <option value="2">2</option> </select> </div> </div> <div class="col-sm-3"> <div class="form-group"> <label class="">Date:</label> <input class="" type="text" formControlName="date"> </div> </div> <div class="col-sm-3"> <div class="form-group"> <label class="">Amount:</label> <input class="" type="text" formControlName="amount"> </div> </div> </div> </div>

here is the func to patch array.这是修补数组的函数。

 patchProjectArray(pArray) { const array = pArray.map(data => { return this.fb.group({ type: data.type, date: data.date, amount: data.amount, }) }) const studFormArray: FormArray = this.fb.array(array); this.form.setControl('project', studFormArray); }

Now when I add a new array object its get added at the last index and displays also at last, I want to just display the last added object at top.现在,当我添加一个新数组 object 时,它会添加到最后一个索引并最后显示,我只想在顶部显示最后添加的 object。 How can i do that?我怎样才能做到这一点?

In your addProjectArray(), you can put the new item at the top.在您的 addProjectArray() 中,您可以将新项目放在顶部。

So rather than pushing in the array, you can use the array.unshift method.因此,您可以使用 array.unshift 方法,而不是推入数组。

addProjectArray() {
  this.projectArray.unshift(
    this.fb.group({
      type: ['', Validators.required],
      date: ['', Validators.required],
      amount: ['', Validators.required],
    })
  );
}

Use Insert It will let you add a new AbstractControl at the given index in the array.使用Insert它将允许您在数组中的给定索引处添加一个新的 AbstractControl。

Try this:尝试这个:

addProjectArray() {
  this.projectArray.insert(0,
    this.fb.group({
      type: ['', Validators.required],
      date: ['', Validators.required],
      amount: ['', Validators.required],
    })
  );
}

Example例子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM