来自Select Option的Angular2 POST数据

时间:2017-04-13 10:07:11

标签: angular

我想从我从选定选项中获取的值将数据发布到我的API。但我得到undefined值。

这是我的模特: -

export class Process {
    id: number;
    class_id: string;
    notes: string;
    levelling_route: string;
    code: string;
  }

这是我的帖子功能

PostData(process: Process) {
        this.jobService.PostLevelling(process).subscribe(
          data=> this.event = JSON.stringify(data),
          error=> alert(error),
          () => console.log("Successful Post Data")
        )

        }

我的HTML

<select  class="form-control" id="select" name="code" size="1">
<option *ngFor="let obj of code" [(ngModel)]="code" name="code" [ngValue]={{obj?.id}} ngDefaultControl>{{obj?.name}}</option>
</select>

我错过了什么?

3 个答案:

答案 0 :(得分:0)

问题在于您绑定到模型的位置。将[(ngModel)]="code"移至<select>标记。

看看the docs

答案 1 :(得分:0)

请尝试这个..我得到了你的问题


$ sudo chown -R mongod:mongod \
      /var/run/mongodb/mongod.pid \
      /tmp/mongodb-27017.sock \
      /var/lib/mongo/

或使用这种方式

Error reading file /path/file: Permission denied

答案 2 :(得分:0)

我认为它应该是这样的

<select class="form-control" id="select" name="code" [(ngModel)]="code" size="1">
    <option *ngFor="let obj of code" [value]="obj?.id">{{obj?.name}}</option>
</select>
相关问题