要解决Angular ng-select在动态控件上无法预先选择值的问题,可以使用以下方法:
import { Component, ViewChild, AfterViewInit } from '@angular/core';
import { NgSelectComponent } from '@ng-select/ng-select';
@Component({
selector: 'app-my-component',
template: `
`
})
export class MyComponent implements AfterViewInit {
@ViewChild('mySelect') mySelect: NgSelectComponent;
options = ['Option 1', 'Option 2', 'Option 3'];
selectedValue = 'Option 2';
ngAfterViewInit() {
this.mySelect.writeValue(this.selectedValue);
}
}
import { Component, ViewChild } from '@angular/core';
import { NgSelectComponent } from '@ng-select/ng-select';
@Component({
selector: 'app-my-component',
template: `
`
})
export class MyComponent {
@ViewChild('mySelect') mySelect: NgSelectComponent;
options = ['Option 1', 'Option 2', 'Option 3'];
selectedValue = 'Option 2';
ngAfterViewInit() {
setTimeout(() => {
this.mySelect.setValue(this.selectedValue);
});
}
}
以上是三种常见的解决方法,根据具体的情况选择适合的方法来解决问题。