简体   繁体   English

Angular 6 FormArrays中的RadioButtons-维护form.value的结构

[英]RadioButtons in Angular 6 FormArrays - maintaining the structure of form.value

I'm trying to get along with FormArrays and radio buttons in Angular 6, and somehow cannot get what I want. 我试图与Angular 6中的FormArrays和单选按钮相处,并且某种程度上无法获得我想要的东西。

I want to initialize the form values based on JSON-like object (server response) - this part is mostly working - and at the same time - get the same object structure back - which is where I have problem. 我想基于类似JSON的对象(服务器响应)初始化表单值-这部分大部分工作-并且同时- 返回相同的对象结构 -这是我遇到的问题。

Instead I see the form.value changes the supplied property from boolean to string (an option's id). 相反,我看到form.value将提供的属性从布尔值更改为字符串(选项的ID)。

https://stackblitz.com/edit/angular-checkbox-radio https://stackblitz.com/edit/angular-checkbox-radio

I also want to be able to patch a value onto an existing form, such as for editing. 我还希望能够将值修补到现有表单上,例如进行编辑。

I would like to do it in a declarative and reactive way, without resorting to ngModel or other mixing of techniques - if possible. 我想以声明式和反应式的方式进行操作,如果可能的话,不求助于ngModel或其他技术混合。

What/Where am I doing wrong?, please help me before I yarn add ng-formly in desperation... 我在哪里/哪里做错了?请在帮助我绝望地yarn add ng-formly之前帮助我。

You can add the fields that you want in the final value of the form in the buildSkills and buildLevels methods: 您可以在buildSkillsbuildLevels方法的表单最终值中添加所需的字段:

buildSkills() {
  const arr = this.user.skills.map(skill => {
    return this.fb.group({
      name: skill.name,
      id: skill.id,
      selected: skill.selected,
    });
  });
  return this.fb.array(arr);
}

buildLevels() {
  const arr = this.user.levels.map(level => {
    return this.fb.group({
      name: level.name,
      groupId: level.groupId,
      id: level.id,
      selected: level.selected,
    });
  });
  return this.fb.array(arr);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM