ngModel未在select元素angular2中选择任何选项

时间:2017-12-11 12:10:00

标签: angular angular-ngmodel

我在选择选项中使用ngModel但它无法正常工作。默认情况下,使用ngModel未选择任何值。

样品:

<div class="form-group">
    <label class="control-label" for="select">Client Name</label>
    <div class="input-group">
        <select class="form-control" id="select" [(ngModel)]="selectedFeesEntry.client" name="selectedFeesEntry.client.name" (change)="onClientNameChange(selectedFeesEntry.client.name)">
            <option *ngFor="let cl of clientList" [ngValue]="cl">{{cl.name}}</option>
        </select>
    </div>
</div>

这里, clientList 是一个对象数组,我正在使用Web服务来推送此数组中的元素。 selectedFeesEntry 是一个对象,并为此对象使用其他Web服务。默认情况下,加载组件时应选择默认值。

样本TS:

selectedFeesEntry: any;
clientList: Array<any> = [];

    getFeesData(headers){
        this.logsProvider.getAllFeeEntries(headers)
        .subscribe(
            data => {
                this.selectedFeesEntry = JSON.parse(data['_body']);
            },
            error => {
            }
        );
    }

    getClients(headers){
        this.logsProvider.getAllClients(headers)
        .subscribe(
            data => {
                this.clientList = JSON.parse(data['_body']); 
            },
            error => {
            }
        );
    }

示例Json:

  1. selectedFeesEntry - &gt;
  2. {
        "id":1,
        "date":"2017-08-05 03:01:00",
        "amount":500.0,
        "client":
        {
            "id":21,
            "name":"client Fee1",
            "addressee":"xyz",
            "address1":"address1 "
        }
    }
    
    1. clientList - &gt;
    2. [
         {
            "id":8,
            "name":"client1 updated",
            "addressee":"client1",
            "address1":"new add "
         },
         {
            "id":14,
            "name":"client4",
            "addressee":"xyz",
            "address1":"some street at some city updated ",
         },
         {
            "id":18,
            "name":"new client",
            "addressee":"abc",
            "address1":"add1",
         }
      ]
      

      我不知道我哪里出错了。

      普兰克:https://stackblitz.com/edit/angular-rpllrj?embed=1&file=app/app.component.css

      任何帮助都将不胜感激。

0 个答案:

没有答案