如何使用Dart在angular2中绑定ngFormModel?

st_clair_clarke

以下以表格形式工作(显示表格)

.html

  <form (ngSubmit) = "onSubmit()"
         #nameForm = "ngForm">
    {{diagnostic}}
    <div class = "mdl-card mdl-shadow--3dp layout horizontal wrap">
      <div class = "mdl-card__title">
        <h2 class = "mdl-card__title-text">Name</h2>
      </div>

      <div
          class = "mdl-textfield mdl-js-textfield mdl-textfield--floating-label">
        <input
            required
            type = "text"
            [(ngModel)] = "name.first"
            ngControl = "first"
            #first = "ngForm"
            (input)="onInputHandler($event)"
            class = "mdl-textfield__input ng-valid"
            id = "first">
        <label
            class = "mdl-textfield__label"
            for = "first">First</label>
        <span [hidden] = "isFirstValid"
              class =  "mdl-textfield__error">{{firstErrorMsg}}</span>
      </div>

      <div class =
               "mdl-card__actions mdl-card--border">
        <button id = "startButton"
                class = "mdl-button mdl-js-button mdl-button--raised mdl-button--accent mdl-js-ripple-effect"
        >Submit
        </button>

    <br>
    <button class = "mdl-button mdl-js-button mdl-button--raised mdl-button--colored">
      Button All
    </button>

  </form>

尝试按照Angular 2.0中的表单和验证中的示例进行操作

我无法通过将表单的第一行更改为来显示UI

<form (ngSubmit) = "onSubmit()"
        [ngFormModel]="form" #f="form">
    {{diagnostic}}
..

进行更改后,浏览器根本无法显示任何内容,就好像它无法解析标记一样-错误实际上以pub-serve或debug模式显示。

Transform TemplateCompiler on epimss_ng2_reg|lib/components/name_component.ng_meta.json threw error: Template parse errors:
There is no directive with "exportAs" set to "form" ("
<div [hidden] = "isSubmitted">
  <form (ngSubmit) = "onSubmit()"
        [ng-form-model]="form" [ERROR ->]#f="form">
    {{diagnostic}}
    <div class = "mdl-card mdl-shadow--3dp layout horizontal wrap">
"): NameComponent@12:31
....

为什么这不起作用?

贡特·佐赫鲍尔(GünterZöchbauer)

自从创建博客帖子以来,这似乎已被更改。NgForm现在导出为ngForm而不是form

[ngFormModel]="form" #f="ngForm">

GitHub来源中是正确的,但在博客文章中却不正确

根据Dart博客文章中的示例,完整组件

@Component(selector: 'form-element')
@View(template: '''
<h1>form-element</h1>
<form (ngSubmit)="onSubmit()" [ngFormModel]="form" #f="ngForm">
    <div>
        <div class="formHeading">First Name</div>
        <input type="text" id="firstName" ngControl="firstName">
        <div class="errorMessage" *ngIf="f.form.controls['firstName'].touched && !f.form.controls['firstName'].valid">First Name is required</div>
    </div>
    <div>
        <div class="formHeading">Street Address</div>
        <input type="text" id="firstName" ngControl="streetAddress">
        <div class="errorMessage" *ngIf="f.form.controls['streetAddress'].touched && !f.form.controls['streetAddress'].valid">Street Address is required</div>
    </div>
    <div>
        <div class="formHeading">Zip Code</div>
        <input type="text" id="zip" ngControl="zip">
        <div class="errorMessage" *ngIf="f.form.controls['zip'].touched && !f.form.controls['zip'].valid">Zip code has to be 5 digits long</div>
    </div>
    <div>
        <div class="formHeading">Address Type</div>
        <select id="type" ngControl="type">
            <option [value]="'home'">Home Address</option>
            <option [value]="'billing'">Billing Address</option>
        </select>
    </div>
    <button type="submit" [disabled]="!f.form.valid">Save</button>
    <div>
        <div>The form contains the following values</div>
        <div>
            {{payLoad}}
        </div>
    </div>
</form>
''')
class FormElement {
  ControlGroup form;
  String payLoad;
  FormElement(FormBuilder fb) {
    form = fb.group({
      "firstName": ['', Validators.required],
      "streetAddress": ['', Validators.required],
      "zip": [
        '',
        Validators.compose([ZipValidator.validate])
      ],
      "type": ['home']
    });
  }

  void onSubmit() {
    payLoad = JSON.encode(this.form.value);
  }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章