87,907
社区成员
发帖
与我相关
我的任务
分享
<div class="form-group" id="steps">
<label for="productionStep">生产工艺步骤</label>
<select class="form-control inline" name="productionStepId" [(ngModel)]="productionStepId" (change)="addStep()"
*ngIf="productionSteps">
<option [value]="0">(未选择)</option>
<option *ngFor="let productionStep of productionSteps" [value]="productionStep.id">{{ productionStep.name }}</option>
</select>
</div>
addStep(){
var htmlStr = '<select class="form-control inline" name="productionStepId" [(ngModel)]="productionStepId" (change)="addStep()" *ngIf="productionSteps">'
+'<option [value]="0">(未选择)</option>'
+'<option *ngFor="let productionStep of productionSteps" [value]="productionStep.id">{{ productionStep.name }}</option>'
+' </select>';
var select=document.createElement("select");
select.innerHTML=htmlStr;
let steps= document.getElementById("steps");
steps.appendChild(select);
}
解决这个很简单呀:如果steps.push()顺序不对就改steps.unshift()。 并且新的元素是在老的前面,而不是后面
并且新的元素是在老的前面,而不是后面
[quote=引用 3 楼 xzy21com 的回复:] 要添加一个一模一样的select你就在外层再添加一个数组做一下*ngFor呀 <select class="form-control inline" *ngFor="let select of selectArray" 然后去修改selectArray
<ng-container *ngFor="let step of steps;let i=index" >
<select class="form-control inline" [(ngModel)]="steps[i]" (change)="addStep()">
<option [value]="0">(未选择)</option>
<option *ngFor="let productionStep of steps[i]" [value]="productionStep.id">{{ productionStep.name }}</option>
</select>
</ng-container>
已经实现选择之后会动态增加一个新的select,但是有时候会出现新的SELECT 绑定的列表不对的情况。[/quote]
并且新的元素是在老的前面,而不是后面要添加一个一模一样的select你就在外层再添加一个数组做一下*ngFor呀 <select class="form-control inline" *ngFor="let select of selectArray" 然后去修改selectArray
<ng-container *ngFor="let step of steps;let i=index" >
<select class="form-control inline" [(ngModel)]="steps[i]" (change)="addStep()">
<option [value]="0">(未选择)</option>
<option *ngFor="let productionStep of steps[i]" [value]="productionStep.id">{{ productionStep.name }}</option>
</select>
</ng-container>
已经实现选择之后会动态增加一个新的select,但是有时候会出现新的SELECT 绑定的列表不对的情况。MVVC框架基本是不会再去操作dom的。你的 addStep方法里应该只是修改productionSteps的值即可。