0

我是我的 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
            }
          }
4

1 回答 1

0

您有几个选项,但最简单的方法是简单地设置options为 observable。

// 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) {}
于 2021-01-19T16:46:54.007 回答