为什么此Angular Select列表中的值未更新?

时间:2018-05-18 13:21:03

标签: angular drop-down-menu

我正在尝试创建一个人员选择列表。

     <div *ngIf="people">
        <label>Tuoja</label>
        <select [(ngModel)]="person" name="person" required>
          <option *ngFor="let person of people" [value]="person.PersonID">
            {{person.FirstName}} {{person.LastName}}
          </option>
        </select>
      </div>

提交表单后,它会将ngForm对象发送到typescript类:

  sendNewMovie(movieForm: NgForm): void {
     console.log(movieForm.value.person); // this is object
     console.log(movieForm.value.movieName); // this is string
     console.log(movieForm.value.movieDetails); // this is string
}

sendMovie函数在控制台中给出了这个,第一个控制台日志未定义,但其他日志工作:

undefined // this doesn't work

moviename // this works

moviedetails // this works

1 个答案:

答案 0 :(得分:0)

修正了它

<div *ngIf="people">
    <label>Tuoja</label>
    <select [(ngModel)]="person" name="person" required>
      <option *ngFor="let person of people" [value]="person.PersonID">
        {{person.FirstName}} {{person.LastName}}
      </option>
    </select>
  </div>

在组件中

console.log(movieForm.value.person);
console.log(movieForm.value.movieName);
console.log(movieForm.value.movieDetails);

输出

2

moviename

moviedetails