Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Typescript 角度2+;在调用后选择模式中更新值_Typescript_Angular2 Forms_Two Way Binding - Fatal编程技术网

Typescript 角度2+;在调用后选择模式中更新值

Typescript 角度2+;在调用后选择模式中更新值,typescript,angular2-forms,two-way-binding,Typescript,Angular2 Forms,Two Way Binding,在将select元素添加到模态窗口后,将其默认值设置为select元素有问题。下面是一段可能有助于理解错误的代码。 这是我的选择 <select [disabled]="electricalItem.equipmentType.length == 0" id="selectedEquipmentType" class="form-control selectForOpenModal" [(ngModel)]="electricalItem.selectedEq

在将select元素添加到模态窗口后,将其默认值设置为select元素有问题。下面是一段可能有助于理解错误的代码。 这是我的选择

  <select 
   [disabled]="electricalItem.equipmentType.length == 0"
   id="selectedEquipmentType"
   class="form-control selectForOpenModal"
   [(ngModel)]="electricalItem.selectedEquipmentType"
   name="selectedEquipmentType"
  >
   <option
    *ngFor="let currentEquipmentType of electricalItem.equipmentType" 
    [ngValue]="currentEquipmentType"
    [selected]="currentEquipmentType==electricalItem.selectedEquipmentType"
   >
    {{currentEquipmentType}}
   </option>
  </select>
  <button
   type="button"
   class="btn btn-outline-secondary btnPlus fa fa-plus"
   data-toggle="tooltip"
   data-placement="top"
   title="Add new item"
   (click)="equipmentTypeModal.showAddModal()"
  ></button>
  <button
   type="button"
   class="btn btn-outline-secondary btnMinus fa fa-minus"
   data-toggle="tooltip"
   data-placement="top"
   title="Drop selected item"
   (click)="equipmentTypeModal.showDropModal(dropElementFlag)"
   [disabled]="electricalItem.equipmentType.length == 0"
  ></button>
推送法

 public addItem(itenEl: string){
  this.dataValue.push(itenEl);
  this.selectedValue = itenEl;
  this.visible = false;
  setTimeout(() => this.visible = false, 300);
  this.itenElement = null;}
因此,现在是展示结果的时候了。

之后


因此,如果你现在能解决这个问题,我将不胜感激。谢谢大家。

您能为Debugger创建一个plunker或stackblitz演示吗删除
[选定]=“currentEquipmentType==electricalItem.selectedEquipmentType”
。真理的源泉是NGM模型。如果ngModel(即electricalItem.selectedEquipmentType)是当前设备类型,则选择该选项。因此,您需要确保electricalItem.selectedEquipmentType确实是currentEquipmentType之一的
=
。@JBNizet删除[selected]元素不会给出结果(((添加(ngModelChange)的尝试)=“onChange($event)”只有当下拉列表被激活时才会调用,可能我不太了解你。你能举一个例子说明你解决这个问题的想法吗?要解决这个问题,你需要确保electricalItem.selectedEquipmentType确实是===到currentEquipmentType中的一个。你已经发布了很多不相关的代码,但没有发布该代码at初始化数组
electricalItem.equipmentType
,以及值
electricalItem.selectedEquipmentType
。这才是重要的:后者应该是前者的一个元素。@jbnite您能给我举一些例子吗?
  @Input() dataValue;@Input() selectedValue;@Input() title: string;
 public addItem(itenEl: string){
  this.dataValue.push(itenEl);
  this.selectedValue = itenEl;
  this.visible = false;
  setTimeout(() => this.visible = false, 300);
  this.itenElement = null;}