如何将 templateOptions.options 用于 ngx 形式的多复选框字段

问题描述

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

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...