Angular 2 将表单分配给变量不起作用#f ="form"(提交)="onSubmit(f.value)"

标签 angular meteor angular2-forms angular2-directives

我刚刚完成了 meteor angular 2 教程。在 step 4使用散列 f 等于 form 的 Angular 2 表单绑定(bind)用于将表单绑定(bind)到变量 f,然后绑定(bind) onSubmit 函数。两者都不适合我。 Angular 2 API 有变化吗?

HTML 无效:

<form #f="form" (submit)="addParty(f.value)">
  <div>{{f.value}}</div>
</form>

样本中 parties-form.html 的原始 HTML 代码:

<form [ng-form-model]="partiesForm" #f="form" (submit)="addParty(f.value)">
  <label>Name</label>
  <input type="text" ng-control="name">
  <label>Description</label>
  <input type="text" ng-control="description">
  <label>Location</label>
  <input type="text" ng-control="location">
  <button>Add</button>
  <div>{{f}}</div>
  <div>{{f.value}}</div>
</form>

和 JS 组件 parties-form.ts:

/// <reference path="../../typings/angular2-meteor.d.ts" />

import {Component, View} from 'angular2/angular2';

import {FORM_DIRECTIVES, FormBuilder, Control, ControlGroup, Validators} from 'angular2/angular2';

import {Parties} from 'collections/parties';

@Component({
    selector: 'parties-form'
})
@View({
    templateUrl: 'client/parties-form/parties-form.html',
    directives: [FORM_DIRECTIVES]
})
export class PartiesForm {
    partiesForm: ControlGroup;

    constructor() {
        var fb = new FormBuilder();
        this.partiesForm = fb.group({
            name: ['', Validators.required],
            description: [''],
            location: ['', Validators.required]
        });
        // Test
        console.log(this.partiesForm.value);
    }

    addParty(party) {
        console.log("assParty", party);
        return true;
        if (this.partiesForm.valid) {
            Parties.insert({
                name: party.name,
                description: party.description,
                location: party.location
            });

            (<Control>this.partiesForm.controls['name']).updateValue('');
            (<Control>this.partiesForm.controls['description']).updateValue('');
            (<Control>this.partiesForm.controls['location']).updateValue('');
        }
    }

}
console.log("PartiesForm loaded");

我复制了 meteor angular 2 示例,查看那里的确切代码。其他样本,如 ng-book也将其用作 onSubmit 函数

#f="form" (submit)="onSubmit(f.value)"

最佳答案

问题是缓存问题。通过本教程,第一个版本是缓存。不确定,但我认为 meteor 应该自动清除缓存,但我需要手动删除缓存才能使其正常运行。

关于Angular 2 将表单分配给变量不起作用#f ="form"(提交)="onSubmit(f.value)",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34121490/

相关文章:

meteor - 如何使用 Meteor 实现 PeerJS 服务器?

html - Angular2/4 电话验证

Angular + Jasmine : How to ignore/mock one function in the tested component (not in a dependency)?

css - 预定义样式不会改变

javascript - 将脚本 JS 添加到我的 angular7 项目中

validation - Angular 2 Final 中的最小/最大验证器

Angular2 Reactive form.reset()不清除自定义组件

angular - 在 Ionic 3 中定义模型的正确方法

Meteor:如何创建后插入 Hook ?

javascript - javascript中空方括号代表什么变量值?