【发布时间】:2019-12-02 08:49:23
【问题描述】:
嗨,目前我已经使用 angular 5 和 typescript 开发了 UI。 在我的一个组件中,我有一个反应式表单,在组件视图中,我有选择框表单控件。所以我想要的是每当我从选择框中选择不同的选项时,它应该打印选择的值。所以我在我的组件类中注册了一个函数contactListChanged。
<select formControlName="contactList" [compareWith]="compareResource" (change)="contactListChanged($event)">
<option value="" disabled>{{ 'PLACEHOLDERS.CONTACT_LIST' | translate }}</option>
<option *ngFor="let contactList of contactLists" [ngValue]="contactList">{{ contactList.name }}</option>
</select>
在组件 ts 文件中
contactListChanged($event){
let a = $event.target.value ;
console.log(a);// prints 1: Object
console.log(this.contactListControl.value); // prints Object {id: "1", name: "irish msisdn 1", total: 1000, displayName: "irish msisdn 2(1000)"}
}
console.log(a) 打印值“2:对象”。但是 console.log(this.contactListControl.value);正确打印值。 对象{id:“2”,名称:“irish msisdn 2”,总数:1200,displayName:“irish msisdn 2(1200)”}
为什么会出现这种行为?我希望 console.log(a) 打印值 对象{id:“2”,名称:“irish msisdn 2”,总数:1200,displayName:“irish msisdn 2(1200)”}
所以我想要的是使用 [ngValue] 并在方法 contactListChanged 中获取所选对象。我怎样才能实现它?
非常感谢您的帮助
谢谢你
【问题讨论】:
-
可以添加console.log的画面吗?
-
1: Object Object {id: "1", name: "irish msisdn 1", total: 1000, displayName: "irish msisdn 2(1000)"}
标签: angular typescript