Angular 2 form, get method istead of post -
while working on angular 2 form submit run problem. when create object inside component works , form gets submit via post method. when using object class outside component form sends request url http://localhost:4200/blog?title=sss&content=ssssss
does know why happening?
template:
<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 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 3 znaki).</span> </div> <button type="submit" class="btn btn-primary" [disabled] ="!f.valid" >wyślij</button> </form> component:
import {component, oninit, viewchild} '@angular/core'; import {ngform} "@angular/forms"; import {blog} "../../shared/blog"; import {blogservice} "../../services/blog.service"; @component({ selector: 'app-blog-form', templateurl: './blog-form.component.html', styleurls: ['./blog-form.component.css'] }) export class blogformcomponent implements oninit { @viewchild('f') form: ngform; errormessage: string; /* works blog = { title: '', content: '', datecreated: '' }*/ //this doesn't work blog: blog; ngoninit(){} onsubmit(){ this.blog.title = this.form.value.title; this.blog.content = this.form.value.content; } } the blog class. tried both this:
export class blog { constructor(public title = '', public content = '', public datecreated = ''){}} and this:
export class blog { constructor(public title : string, public content : string, public datecreated : string){}} thanks :)
i not sure why happening try not using this.form.value.
onsubmit(){ this.blog.title = this.form.title; this.blog.content = this.form.content; console.log(this.blog); } use of value posts page. code should work.
Comments
Post a Comment