使用 ember-bootstrap 自定义元素改变变更集字段值

Mutate changeset field value with ember-bootstrap custom element

我在 Ember 3.15 (Octane)。使用以下设置

模板

<MyComponent
  @changeset={{changeset this.model this.MyValidationClass}}
  @onSubmit={{this.save}}
/>

分量

<BsForm {{on 'submit' (fn this.submit @changeset)}}
  @model={{@changeset}}
  @formLayout="horizontal"
  as |form|>

  <form.element
    @label="My Custom Field"
    @property="myField" as |el|>

    <BsButtonGroup
      @value="{{el.value}}"
      @type="radio"
      @onChange="{{action (changeset-set @changeset 'myField') 'target.value'}}" 
      as |bg|>

      {{#each @changeset.options as |option|}}      // options is a string array ['first', 'second', 'third']
        <bg.button
          @type="info"
          @value="{{option}}">
          {{option}}
        </bg.button>
      {{/each}}

    </BsButtonGroup>
  </form.element>
</BsForm>

我面临的问题是,当我更改单选按钮选择时,changeset-set 没有更改 myField 的值。我试过了

@onChange="{{action (mut el.value)}}

@onChange="{{action (changeset-set @changeset 'episodeType') this.target.value}}"

@onChange="{{action (changeset-set @changeset 'episodeType') this.buttonGroupValue}}"

如评论中所述,您的代码段中存在一些格式错误的模板代码。

不应将卷曲用双引号括起来,因为它应被视为动态内容而不是字符串。例如,代码应该是 @onChange={{action (mut el.value)}} 而不是 @onChange="{{action (mut el.value)}}"

修复模板应该会有帮助。