简体   繁体   English

新表格如何在angular2中工作

[英]How do new forms work in angular2

The best tutorials I can find out there, crucially, this one .... only work for the deprecated (and soon to be completely gone) forms stuff. 我可以找出有最好的教程,关键的是, 这一个 ....只为弃用(和即将完全消失)形成的东西的工作。

As far as I can tell this is the only documentation on the new forms: https://docs.google.com/document/u/1/d/1RIezQqE4aEhBRmArIAS1mRIZtWFf6JxN_7B4meyWK0Y/pub 据我所知,这是新表格上的唯一文档: https : //docs.google.com/document/u/1/d/1RIezQqE4aEhBRmArIAS1mRIZtWFf6JxN_7B4meyWK0Y/pub

I managed to get the @angular/forms installed (with help from here ) and can get a single-level form to work by copy-pasting the examples in docs from above... however, I'm having a hell of a time implementing anything else or anything that doesn't define the entire form structure at compile time. 我设法安装了@ angular / forms(在这里获得了帮助),并且可以通过从上面复制文档中的示例来获取单层表单来工作...但是,我真是一团糟在编译时实现其他任何东西或未定义整个表单结构的任何东西。

I'm hoping to build an app that allows me to drag-drop and remove form questions. 我希望构建一个允许我拖放和删除表格问题的应用程序。 Further, some of the questions will have sub-questions as well and I'd also like to pull the questions from a service (and thus won't be able to hard-code the form layout ahead of time as the docs show) 此外,某些问题也将包含子问题,我也想从服务中提取问题(因此,如文档所示,将无法提前对表单布局进行硬编码)

It seems that, at least for my use case, it would be best to have one component for each 'question'. 看来,至少对于我的用例而言,最好为每个“问题”都使用一个组件。 Each component would then have it's own form in it's template. 每个组件将在模板中具有其自己的形式。

That is, I'd like ParentComponent to have it's template look something like this: 也就是说,我希望ParentComponent的模板看起来像这样:

<h1>Parent Component</h1>

<form (submit)="onSubmit()" *ngFor='let question of questions'>
    <subform [question]='question'></subform>
</form>

And then in the subform we'd build each template based on what "Question" got sent in. For the sake of an example, something like (apologies for the fact this probably contains errors, I'm writing it directly in here... will try to plunkr later): 然后,在子表单中,我们将根据发送的“问题”构建每个模板。为示例起见,(对于可能包含错误的事实表示歉意,我直接在此处编写。 。稍后将尝试“ punkr”):

import {Component, Input} from '@angular/core';

@Component({
    selector: 'subform',
    template: `
        <h3>{{question.label}}</h3>
        <form *ngFor='let option of question.getOptions()'
            <input type="radio" name="gender" value="[option]" checked> {{option}}<br>
       </form>
    `
})
export class ChildComponent {
    @Input() question
 }

I can't seem to figure out the syntax to make something like this work AND give me references so I could update data as the form options changed. 我似乎无法弄清楚执行此操作所需的语法并给我引用,以便我可以在更改表单选项时更新数据。 Every page or tutorial on this subject seems to give different answers (including the angular documentation of forms (which gives "you are using deprecated stuff" warnings)... and all are different than the office change docs above. 关于该主题的每个页面或教程似乎都给出不同的答案(包括表格的成角度的文档 (给出“您正在使用不赞成使用的东西”警告)……)并且都与上述Office更改文档不同。

FormGroup seems to be the place I should be looking, but that appears to require the whole form structure be built at compile time... (the data can be updated later according to docs if you search for "Bind existing form with async one-way binding"... but I can't seem to make building the form structure happen dynamically at runtime) FormGroup似乎是我应该寻找的地方,但这似乎需要在编译时构建整个表单结构...(如果您搜索“以异步方式绑定现有表单,方式绑定” ...但我似乎无法使表单结构的构建在运行时动态发生)

check this out: https://angular.io/docs/ts/latest/cookbook/dynamic-form.html 检查一下: https : //angular.io/docs/ts/latest/cookbook/dynamic-form.html

maybe it can help you 也许可以帮到你

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

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