gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 18:00:08 25 4
gpt4 key购买 nike

我刚刚完成了 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com