问题描述
我是我的 Angular 应用程序,我需要从我的应用程序(组件或服务)内的函数中设置多复选框字段的选项。
我不知道 templateOptions.options
是如何工作的,我应该如何写值?
这是我的json代码:
{
"key": "clientIds","type": "multicheckBox","className": "flex-1","templateOptions": {
"label": "Clients","required": true
},"expressionProperties": {
"templateOptions.options": "??????" // <-- here to bind with a component or shared service method
}
}
解决方法
您有几个选项,但最简单的方法是将 options
设置为可观察对象。
// Client Service
import { Injectable } from '@angular/core';
import { Observable,of } from 'rxjs';
@Injectable()
export class ClientService {
clients = [
{ id: '1',name: 'Michael' },{ id: '2',name: 'GammaStream' },];
getClients(): Observable<any[]> {
return of(this.clients);
}
}
// component
fields: FormlyFieldConfig[] = [
{
key: 'clientIds',type: 'multicheckbox',className: 'flex-1',templateOptions: {
label: 'Clients',required: true,options: this.clientService.getClients()
}
}
];
constructor(private clientService: ClientService) {}