Angular 4 FormControl自动完成初始数据

时间:2017-10-06 09:47:03

标签: angular typescript

我正在为我的应用程序使用material.angular.io组件 - 特别是自动完成。我正在修改它以用作多选,现在我遇到加载初始值的问题:

代码:

export class CaseActivityTimeEditComponent implements OnInit {
    public clients: Client[];
    clientControl: FormControl = new FormControl();

    getClients(): void {
        this.clientService.getClients(null,null)
            .subscribe(data => {
                this.clients = data;
            });
    }

    constructor(private clientService: ClientService) { }

    ngOnInit(): void {
        this.getClients();

        this.filteredClients = this.clientControl.valueChanges
            .startWith(this.clients) ==> this.clients is UNDEFINED
            .map(val => val ? this.filterClient(val) : this.clients);

    }
}

基本上,我想填充此 FormControl 的初始值,但this.getClients()需要一段时间才能获得初始值。

使这项工作的最佳做​​法是什么?

1 个答案:

答案 0 :(得分:0)

可能的解决方案:

getClients(): void {
    this.clientService.getClients(null,null)
        .subscribe(data => {
            this.clients = data;
            // realize your auto complete function here because 
            // 'this.clients' is now defined
        });
}