当用户从选项下拉列表中加载不同的选择(对象)时,我想获得在(更改)函数中返回的新对象。但是,即使选择正确更改,它每次都返回默认对象。使用 $scope 时,这种类型的绑定在 AngularJs 中有效。不确定我的 Angular 11 ngModel 等是否正确。(我已经导入了 FormsModule)
组件.ts...
public selectedQuery: any;
// default object...
public myQueries: [{
myQueryId: number,
myQueryCreatedDate: Date,
myQueryString: string
}] = [{
myQueryId: 0,
myQueryCreatedDate: new Date(),
myQueryString: "Please select a query"
}]
dropDownListBuild(); // function builds correctly, loads myQueries array with options
...
this.selectedQuery = this.myQueries[0]; // gets set as default
...
onQueryChange(query: any) {
console.log(this.selectedQuery + query); // not seeing any change happen
}
组件.html...
<div class="selectClass">
<select [(ngModel)]="selectedQuery" (change)="onQueryChange(selectedQuery)">
<option *ngFor="let opt of myQueries" [ngValue]="selectedQuery"> {{opt.myQueryString}}</option>
</select>
</div>