admin管理员组文章数量:1316841
I am new to Angular 2 and Typescript but I am trying to build a web app. I build some input fields and I want to log them to the console after submitting them via a button. But my button does not really work or react.
Does anyone have an idea what is the problem with my code?
Here is my code:
appponent.html
<div class="panel panel-primary">
<div class="panel-heading">Status</div>
<div class="panel-body">
<h3>{{title}}</h3>
<form #userForm="ngForm" (ngSubmit)="onSubmit(userForm.value)">
<div class="form-group">
<label>Name</label>
<input type="text" class="form-control" name="name" ngModel>
</div>
<div class="form-group">
<label>Email</label>
<input type="text" class="form-control" name="email" ngModel>
</div>
<div class="form-group">
<label>Street</label>
<input type="text" class="form-control" name="street" ngModel>
</div>
<div class="form-group">
<label>PLZ</label>
<input type="text" class="form-control" name="plz" ngModel>
</div>
</form>
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
appponent.ts
import { Component, style } from '@angular/core';
import { TutorialsComponent } from './tutorialponent';
import { SteliosComponent } from './steliosponent';
@Component({
selector: 'app-root',
templateUrl: './appponent.html',
styleUrls: ['./appponent.css']
})
export class AppComponent {
public title ="Das ist die erste Componente"
public childData: string;
onSubmit(value: any){
console.log(value);
}
}
I am new to Angular 2 and Typescript but I am trying to build a web app. I build some input fields and I want to log them to the console after submitting them via a button. But my button does not really work or react.
Does anyone have an idea what is the problem with my code?
Here is my code:
app.ponent.html
<div class="panel panel-primary">
<div class="panel-heading">Status</div>
<div class="panel-body">
<h3>{{title}}</h3>
<form #userForm="ngForm" (ngSubmit)="onSubmit(userForm.value)">
<div class="form-group">
<label>Name</label>
<input type="text" class="form-control" name="name" ngModel>
</div>
<div class="form-group">
<label>Email</label>
<input type="text" class="form-control" name="email" ngModel>
</div>
<div class="form-group">
<label>Street</label>
<input type="text" class="form-control" name="street" ngModel>
</div>
<div class="form-group">
<label>PLZ</label>
<input type="text" class="form-control" name="plz" ngModel>
</div>
</form>
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
app.ponent.ts
import { Component, style } from '@angular/core';
import { TutorialsComponent } from './tutorial.ponent';
import { SteliosComponent } from './stelios.ponent';
@Component({
selector: 'app-root',
templateUrl: './app.ponent.html',
styleUrls: ['./app.ponent.css']
})
export class AppComponent {
public title ="Das ist die erste Componente"
public childData: string;
onSubmit(value: any){
console.log(value);
}
}
Share
Improve this question
asked Nov 21, 2017 at 14:58
ste92ste92
4341 gold badge9 silver badges24 bronze badges
1
- Your button is outside the form. Move it up one line inside the form. – Z. Bagley Commented Nov 21, 2017 at 15:03
3 Answers
Reset to default 6Your button isn't in your form. Put it in your form and you're good to go !
You have to associate the submit button with the form.
This is usually done by putting the <button>
element inside the <form>
element.
You could do that by moving it to just before </form>
.
Alternatively, you could use the form
attribute:
<button form="id_of_form_element_goes_here" type="submit" class="btn btn-primary">Submit</button>
… but browser support for that is weaker.
Move the submit button inside the form.
<form>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
本文标签: javascriptSubmit Button does not workreactStack Overflow
版权声明:本文标题:javascript - Submit Button does not workreact - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742007867a2412323.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论