【问题标题】:How to save checkbox value as string with Formly?如何使用 Formly 将复选框值保存为字符串?
【发布时间】:2021-10-10 09:54:11
【问题描述】:

我目前正在使用 Formly 来生成复选框。勾选复选框时,该值将保存为 true,但我需要将其保存为类似“true”的字符串。我还使用 JSON 来生成复选框,这使得操作 HTML 值变得更加困难。

请参阅此示例:https://stackblitz.com/edit/angular-formly-checkbox?file=src%2Fapp%2Fapp.component.html

有没有办法将值作为字符串而不是布尔值?

【问题讨论】:

  • 我能想到的唯一方法是编写一个自定义的ValueAccessor。但是由于在 Formly 中无法将指令传递给类型,因此您可能还需要编写自定义 Formly 组件。而且由于您使用的是 Material ,因此可能也是一个自定义的 Material 组件。老实说,我觉得这有点奇怪。为什么需要在第一行将布尔值视为字符串?

标签: angular typescript ngx-formly


【解决方案1】:

你可以得到一个 true 值的数组: 为此,只需将 type 设置为 arraytemplateOptions

 templateOptions: {
     type: 'array',
     // other options
 }

【讨论】:

    猜你喜欢
    • 2021-04-07
    • 1970-01-01
    • 1970-01-01
    • 2016-03-27
    • 2020-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-26
    相关资源
    最近更新 更多