gpt4 book ai didi

Angular2(ngSubmit)不起作用

转载 作者:行者123 更新时间:2023-12-02 05:48:10 25 4
gpt4 key购买 nike

我刚刚启动 Angular2 并在添加 时遇到以下错误(ngSubmit) 归因于我的表单

Template parse errors:
There is no directive with "exportAs" set to "ngForm" ("


<form [ERROR ->]#form="ngForm" (ngSubmit)="onSubmit()" novalidate>

以下是我在 package.json 中的一些依赖项。
"dependencies": {
"@angular/common": "2.0.0",
"@angular/compiler": "2.0.0",
"@angular/core": "2.0.0",
"@angular/forms": "2.0.0",
"@angular/http": "2.0.0",
"@angular/platform-browser": "2.0.0",
"@angular/platform-browser-dynamic": "2.0.0",
"@angular/router": "3.0.0",
"@angular/upgrade": "2.0.0",
"angular2-in-memory-web-api": "0.0.20",
"core-js": "^2.4.1",
"ie-shim": "^0.1.0",
"jquery": "^3.1.0",
"reflect-metadata": "^0.1.3",
"rxjs": "5.0.0-beta.12",
"systemjs": "0.19.27",
"zone.js": "^0.6.23"
}

我遵循了 html 表单。
<form #form="ngForm" (ngSubmit)="onSubmit()" novalidate>

<div class="form-group">
<label>Title</label>
<input [(ngModel)]="model.Title" #title="ngModel" name="title" id="title" type="text" class="form-control" value="">
</div>

<div class="form-group">
<label>Description</label>
<textarea [(ngModel)]="model.Description" #description="ngModel" class="summernote form-control" name="description" id="description"></textarea>
</div>

</form>

并关注 类别.组件文件
import { Component } from '@angular/core';
import { FormBuilder} from '@angular/forms';
import { Category } from '../../../models/cms/Category';

@Component({
selector: 'category',
templateUrl: 'category.template.html'
})
export class CategoryComponent{

model = new Category("dummyTitle","dummyDescription");

onSubmit() {
console.log(this.data);
}
}

有人可以指导如何处理这个问题。

最佳答案

您缺少导入文件 import { NgForm } from '@angular/forms';
并且您正在使用模板驱动的表单,因此您不需要 FormBuilder。

试试这个方法

注册-form.component.html

<form #signupForm="ngForm" (ngSubmit)="registerUser(signupForm)">
<label for="email">Email</label>
<input type="text" name="email" id="email" ngModel>

<label for="password">Password</label>
<input type="password" name="password" id="password" ngModel>

<button type="submit">Sign Up</button>
</form>

注册-form.component.ts
import { Component } from '@angular/core';
import { NgForm } from '@angular/forms';

@Component({
selector: 'signup-form',
templateUrl: 'app/signup-form.component.html',
})
export class SignupForm {
registerUser(form: NgForm) {
console.log(form.value);
}
}

关于Angular2(ngSubmit)不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41139546/

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