Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Forms 角度2表单,获取方法istead的post_Forms_Angular_Submit - Fatal编程技术网

Forms 角度2表单,获取方法istead的post

Forms 角度2表单,获取方法istead的post,forms,angular,submit,Forms,Angular,Submit,在处理Angular 2表单提交时,我遇到了一个问题。当我在组件中创建一个对象时,一切都很好,我的表单通过post方法得到submit。但是当我使用组件外部类中的对象时,我的表单会发送一个带有url的get请求 有人知道为什么会这样吗 模板: <form (ngSubmit)="onSubmit()" #f="ngForm"> <!-- <form #f="ngForm" (ngSubmit)="onSubmit(f)">--> <d

在处理Angular 2表单提交时,我遇到了一个问题。当我在组件中创建一个对象时,一切都很好,我的表单通过post方法得到submit。但是当我使用组件外部类中的对象时,我的表单会发送一个带有url的get请求

有人知道为什么会这样吗

模板:

 <form (ngSubmit)="onSubmit()" #f="ngForm">
   <!-- <form #f="ngForm" (ngSubmit)="onSubmit(f)">-->
      <div class="form-group">
        <label for="title">Tytuł</label>
        <textarea class="form-control" id="title" rows="1"
                  ngModel name = "title" required minlength="3" #title="ngModel"></textarea>
        <span class="help-block" *ngIf="!title.valid && title.touched">Wprowadzono za krótki tekst (minum to 3 znaki).</span>
        <label for="content">Zawartość:</label>
        <textarea class="form-control" id="content" rows="3"
                  ngModel name = "content" required minlength="3" #content="ngModel"></textarea>
        <span class="help-block" *ngIf="!content.valid && content.touched">Wprowadzono za krótki tekst (minum to 3 znaki).</span>
      </div>
      <button type="submit" class="btn btn-primary"
              [disabled] ="!f.valid"
      >Wyślij</button>
    </form>
博客类。我尝试了这两种方法:

export class Blog {
  constructor(public title = '', public content = '', public dateCreated = ''){}}
这是:

export class Blog {
  constructor(public title : string, public content : string, public dateCreated : string){}}

感谢您的帮助:)

我不确定为什么会发生这种情况,但请尝试不要使用
this.form.value

onSubmit(){
  this.blog.title = this.form.title;
  this.blog.content = this.form.content;
  console.log(this.blog);
}

使用值返回您的页面。现在,该代码应该可以工作了。

您能提供您创建的服务吗?我不使用该服务。这只是旧的导入。如果我删除导入,问题仍然存在。我得到一个错误:类型“NgForm”上不存在属性“title”
onSubmit(){
  this.blog.title = this.form.title;
  this.blog.content = this.form.content;
  console.log(this.blog);
}