Angular4:第一次选择[selected]不起作用

时间:2018-01-17 06:59:55

标签: angular angular4-forms

有一个代码:

<select name="department" class="form-control select" [(ngModel)]="departments" formControlName="departmentControl">    
    <option *ngFor="let department of departments" [ngValue]="department" [selected]="department.id == this.departmentid">
      {{ department.name }}
    </option>
  </select>

和功能:

isSelected(department): boolean {
  debugger;
  return department.id == this.departmentid;
}

department - 是用户详细信息组件的嵌套组件。在我第一次选择用户详细信息组件后,未选择部门。但第二次一切正常。哪里出错?

2 个答案:

答案 0 :(得分:3)

您正在使用[selected]="department.id == this.departmentid"

时无需:[(ngModel)]

[(ngModel)]="departments"更改为[(ngModel)]="departmentid"

[ngValue]="department"更改为[ngValue]="department.id"

最后看起来应该是这样的:

<select name="department" class="form-control select" [(ngModel)]="departmentid" formControlName="departmentControl">    
    <option *ngFor="let department of departments" [ngValue]="department.id" >
        {{ department.name }}
    </option>
</select>

<强> WORKING DEMO

答案 1 :(得分:1)

您的ngModel错误,您应该将其departmentid设置为ngValue<select name="department" class="form-control select" [(ngModel)]="departmentid" formControlName="departmentControl"> <option *ngFor="let department of departments" [ngValue]="department.id" [selected]="department.id == this.departmentid"> {{ department.name }} </option> </select> 应该是部门的ID,如下所示

TcpClient client = new TcpClient();

client.Connect(resolvedIpAddr, PORT_TLS_11);

Org.BouncyCastle.Security.SecureRandom sr = new Org.BouncyCastle.Security.SecureRandom(); 
TlsProtocolHandler handler = new TlsProtocolHandler(client.GetStream(), sr);

handler.Connect(new MyTlsClient());

有关详情,请参阅 answer