我在页面加载时分配以下数据源'tabNameList'。在加载选择列表之后,我为其分配默认值,例如12。这应该触发下面的代码来触发,但它永远不会触发。如何在第二个加载tablist并且selectedTabId设置为12时触发代码?
设置代码
LoadTabNames(){
this._userProfileService.loadTabNames(this.screenId).subscribe(data => {
this.DisableTab= false;
this.tabNameList = data[0];
this.selectedTabId = data[0]["ScreenId"] <----- TabNameChanged event should fire right here since the ngModel has changed
this.cdr.detectChanges();
})
}
TabNameChanged(event){
this.selectedColumns = [];
this.availableColumns = [];
// When the tab select changes load the new headers into the First ListBox
const detailList = filterBy(this.UserProfileTabData, {
logic: 'and',
filters: [
{ field: 'ScreenId', operator: 'eq', value: this.screenId, ignoreCase: true },
{ field: 'TabId', operator: 'eq', value: event, ignoreCase: true }
]
});
detailList.forEach(element => {
this.selectedColumns.push(element["ColumnName"]);
});
}
我的HTML
<div class="col">
<label for="dropdown2">Tab Name</label>
<select #tab class="custom-select custom-select-lg mb-3" id="dropdown2" [disabled]='DisableTab' (ngModelChange)='TabNameChanged($event)' [(ngModel)]="selectedTabId">
<option *ngFor="let tab of tabNameList let i=index" [(ngValue)]="tab.TabId" >{{tab.TabName}}</option>
</select>
</div>
您需要在HTML中将(ngModelChange)='TabNameChanged($event)'
更改为(change)="TabNameChanged($event)"
以获取下拉列表。
例如:
<div class="col">
<label for="dropdown2">Tab Name</label>
<select #tab class="custom-select custom-select-lg mb-3" id="dropdown2" [disabled]='DisableTab' (change)='TabNameChanged($event)' [(ngModel)]="selectedTabId">
<option *ngFor="let tab of tabNameList let i=index" [(ngValue)]="tab.TabId" >{{tab.TabName}}</option>
</select>
</div>
您可以使用以下代码随时触发元素的事件:
let element = document.getElementById(id);
element.dispatchEvent(new Event("change"));