简体   繁体   English

NgFor在开始时显示第一个元素-Angular 2

[英]NgFor show first element on start - Angular 2

I have row of inputs and dropdowns that I generate each time i click on "Add row" button. 每次单击“添加行”按钮时,都会生成一行输入和下拉列表。 With other button I submit complete data and you can see it in console.log. 使用其他按钮,我提交完整的数据,您可以在console.log中看到它。 as array of objects where each object is practically that row. 作为对象数组,其中每个对象实际上都是该行。

Currently it looks like this: 当前看起来像这样:

在此处输入图片说明

But I need it to look like this: 但是我需要它看起来像这样:

在此处输入图片说明

I use this in popup and I need to show first row immediately when popup opens and every other row should be generated after I click on "Add row" button. 我在弹出窗口中使用它,我需要在弹出窗口打开时立即显示第一行,并且在单击“添加行”按钮后应生成其他所有行。 In the end, when I submit all, first row should be placed first in array and then rest of the generated rows. 最后,当我提交所有内容时,应将第一行放置在数组中,然后放置其余的已生成行。

Is there any way to alter this plunker with index, or there is some other solution? 有什么方法可以用索引更改此插件 ,还是有其他解决方案?

Arrays look like this: 数组如下所示:

 0: {name: "A", data: "123", num: "number1", char: "letter1"}
  1: {name: "B", data: "234", num: "number2", char: "letter2"}
  2: {name: "C", data: "345", num: "number3", char: "letter3"}

You need to implement ngOnInit and add the properties[0] as a empty property, like this: 您需要实现ngOnInit并将properties [0]添加为空属性,如下所示:

ngOnInit() {
  this.properties[0] = this.property;
}

Example on this Plunkr . 有关此Plunkr的示例

Hey I remember your plunkr ! 嘿,我记得你的朋克! I believe I'm the one that corrected it actually. 我相信我是真正纠正它的人。 You should have asked on your other question ! 您应该问其他问题!

To force a value on your selects as default, give a value to the model bound to your select. 要在选择中强制使用默认值,请为绑定到选择的模型提供一个值。 For instance, when you create a new row, set the value of your model to the first option of your selects. 例如,当您创建新行时,将模型的值设置为选择的第一个选项。

Something like this. 这样的事情。

createRow(){
  this.properties.push({ 
    name:"",
    data: "",
    num: this.numberTypes[0],
    char: this.charTypes[0]
  });
}

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

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