我有一个div内容并有一个添加按钮,如果我点击添加按钮,显示/添加相同的内容div。如果我多次点击添加按钮,那多次重复我的div。直到它工作正常,但是当我向任何一个div输入数据时,相同的数据反映在加法div上。任何人都可以帮我解决这个问题。我需要数据在div上,我输入和休息div必须保持为空。
HTML:
<md-card *ngFor="let position of products; let row_ind = index ">
<div>
<md-input-container>
<input mdInput type="text" name="position.workName [(ngModel)]="workDetails.workName"> </md-input-container>
<md-input-container> <input mdInput type="text" name="position.workName" [(ngModel)]="workDetails.workPlace" > </md-input-container>
</div>
<div >
<md-input-container><input mdInput type="text" name="position.workName" [(ngModel)]="workDetails.workUnit"> </md-input-container>
<md-input-container> <input mdInput type="text" name="position.workName" [(ngModel)]="workDetails.workCountry" > </md-input-container> </md-card>
TS:
this.products = [{
"workName": "",
"workPlace":"",
"workUnit":"",
"workCountry":""
}];
open(){
var item = {
"workName": "",
"workPlace":"",
"workUnit":"",
"workCountry":"",
}
this.products.push(item);
}
public products:Array<any>;
export class Work {
public workName:string;
public workPlace: string;
public workUnit: string;
public workCountry :string;
}
Public workDetails:workDetails = new Work();
答案 0 :(得分:1)
我认为要创建一个对象数组,你必须做这样的事情,
TS:
this.products = [{"workName": "", "workPlace": "", "workUnit": "", "workCountry": ""}, {"workName": "", "workPlace": "", "workUnit": "", "workCountry": ""}, {"workName": "", "workPlace": "", "workUnit": "", "workCountry": ""}];
openAddPosition(){
var item = {
"workName": "",
"workPlace":"",
"workUnit":"",
"workCountry":"",
}
this.products.push(item);
}
public products:Array<Work>;
export class Work {
public workName:string;
public workPlace: string;
public workUnit: string;
public workCountry :string;
}
public workDetails:Work = new Work();
HTML:
<button md-mini-fab class="md-fab md-mini add-task" mdTooltipPosition="below" mdTooltip="Add" aria-label="New Task" (click)="openAdd()" style="bottom: 70%; right: 2%;">
<md-icon style="color:white;">add</md-icon>
</button>
<md-card layout="column" class="border-top-3px col-lg-12 col-md-12 col-sm-12 col-xs-12 " *ngFor="let workDetails of products">
<div class="clearfix col-lg-12 col-md-12 col-sm-12 col-xs-12">
<h6 class="color-primary md-headline" style="font-size:18px;">Adding</h6>
</div>
<div class="col-lg-6 col-md-6 col-sm-6 col-xs-6" style="font-size:13px;">
<md-input-container>
<input mdInput type="text" [(ngModel)]="workDetails.workName" placeholder="Work Name" [ngModelOptions]="{standalone: true}" required />
</md-input-container>
<md-input-container>
<input mdInput type="text" [(ngModel)]="workDetails.workPlace" placeholder="Work Name" [ngModelOptions]="{standalone: true}" required />
</md-input-container>
</div>
<div class="col-lg-6 col-md-6 col-sm-6 col-xs-6" style="font-size: 13px;
text-align: left;">
<md-input-container>
<input mdInput type="text" [(ngModel)]="workDetails.workUnit" placeholder="Work Name" [ngModelOptions]="{standalone: true}" required />
</md-input-container>
<md-input-container>
<input mdInput type="text" [(ngModel)]="workDetails.workCountry" placeholder="Work Name" [ngModelOptions]="{standalone: true}" required />
</md-input-container>
</md-card>
注意:
TS:我创建了一系列作品
HTML:添加了for循环并删除了name属性。