为什么我的代码在ANGULAR 5中给出名称属性错误?

时间:2018-08-13 11:10:22

标签: angular

我对Angular 5中的formcontrol有疑问。为什么我的代码在ANGULAR 5中给出名称属性错误?我不明白错误在哪里。我遇到类似Error: No value accessor for form control with unspecified name attribute的错误。您能帮我解决这个问题吗?尽管我在项目中的app.module.ts中添加了导入必不可少的库,但我并未将数据插入api服务。

我的示例json格式

{
    "id": "",
    "name": "",
    "address": "",
    "contactName": "",
    "contactSurname": "",
    "contactPhone": "",
    "secondContactPhone":"",
    "city": {
        "id": 1,
        "name": "asd"
    },
    "town": {
        "id": 2,
        "name": "asd",
        "city": {
            "id": 1,
            "name": "asd"              
        }
    }
}

我的html页面

  <table style="width:500px;">
      <tr><td>New city</td>
        <td>
          <select >
            <option  *ngFor="let sec of bank" [(ngModel)]="city" [name]="name">{{sec?.city?.name}} </option><!--Deneme city -->
          </select>
        </td>
      </tr>
      <tr><td>New city</td>
        <td>
          <select >
            <option  *ngFor="let sec of bank" [(ngModel)]="town">{{sec?.town?.name}}</option><!--Deneme town -->
          </select>
        </td>
      </tr>
    <tr>
      <td>New name:</td>
      <td><input type="text" name="name" [(ngModel)]="name"  ></td>
    </tr>
    <tr>
      <td>New address:</td>
      <td><input  type="text" name="address" [(ngModel)]="address"></td>
    </tr>
    <tr>
      <td>New contactName:</td>
      <td><input  type="text" name="contactName" [(ngModel)]="contactName"></td>
    </tr>
    <tr>
      <td>New contactSurname:</td>
      <td><input  type="text" name="contactSurname" [(ngModel)]="contactSurname"></td>
    </tr>
    <tr>
      <td>New contactPhone:</td>
      <td><input  type="text" name="contactPhone" [(ngModel)]="contactPhone"  ></td>
    </tr>
    <tr>
      <td>New secondContactPhone:</td>
      <td><input type="text" name="secondContactPhone" [(ngModel)]="secondContactPhone"></td>
    </tr>
    <tr>
      <button (click)="Create();">add</button>
    </tr>
  </table>

1 个答案:

答案 0 :(得分:0)

不是一个完整的答案,但我通过此在线https://gist.github.com/keshaliya/a1f1fd5a7b85f72ce1c38d80909faf79检查了您的json格式并对其进行了纠正:

{
        "id": "",
        "name": "",
        "address": "",
        "contactName": "",
        "contactSurname": "",
        "contactPhone": "",
"secondContactPhone":"",
        "city": {
            "id": 1,
            "name": "asd"

        },
        "town": {
            "id": 2,
            "name": "asd",
            "city": {
                "id": 1,
                "name": "asd"                
            }
        }
}