How can I associate a button with a form using Angular?
I'm working with Angular 6.x, and I'd like to associate a submit button located outside the form in the DOm with it. That is, I want to accomplish something structurally equivalent to this:
<button type='submit' form='myform'>
click me!
</button>
<form id='myform' action='#' onsubmit='console.log("wheee")'>
<input type='submit' value='me too'/>
</form>
I.e. I want to handle the submit
event from the form element.
Is there a way to do this without going through nativeElement
or without moving/duplicating the submit handler on the click event of the button?
javascript angular
add a comment |
I'm working with Angular 6.x, and I'd like to associate a submit button located outside the form in the DOm with it. That is, I want to accomplish something structurally equivalent to this:
<button type='submit' form='myform'>
click me!
</button>
<form id='myform' action='#' onsubmit='console.log("wheee")'>
<input type='submit' value='me too'/>
</form>
I.e. I want to handle the submit
event from the form element.
Is there a way to do this without going through nativeElement
or without moving/duplicating the submit handler on the click event of the button?
javascript angular
add a comment |
I'm working with Angular 6.x, and I'd like to associate a submit button located outside the form in the DOm with it. That is, I want to accomplish something structurally equivalent to this:
<button type='submit' form='myform'>
click me!
</button>
<form id='myform' action='#' onsubmit='console.log("wheee")'>
<input type='submit' value='me too'/>
</form>
I.e. I want to handle the submit
event from the form element.
Is there a way to do this without going through nativeElement
or without moving/duplicating the submit handler on the click event of the button?
javascript angular
I'm working with Angular 6.x, and I'd like to associate a submit button located outside the form in the DOm with it. That is, I want to accomplish something structurally equivalent to this:
<button type='submit' form='myform'>
click me!
</button>
<form id='myform' action='#' onsubmit='console.log("wheee")'>
<input type='submit' value='me too'/>
</form>
I.e. I want to handle the submit
event from the form element.
Is there a way to do this without going through nativeElement
or without moving/duplicating the submit handler on the click event of the button?
javascript angular
javascript angular
asked Nov 16 '18 at 3:57
millimoosemillimoose
32.5k763105
32.5k763105
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
You can achieve by reference
#form
of form and passing the same reference to button.
<button type='button' (click)="form.onsubmit()">
click me!
</button>
<form id='myform' #form action='#' onsubmit='console.log("wheee")'>
<input type='submit' value='me too'/>
</form>
Working copy is here - https://stackblitz.com/edit/angular-pnneks
add a comment |
Tyr it like this.
<button type='submit' (click)="myForm.submit()">
click me!
</button>
<form id='myform' action='#' #myForm onsubmit='console.log("wheee")'>
<input type='submit' value='me too'/>
</form>
add a comment |
Well, you could use a Reactive Form and then on the click of this button. call a function that would extract the value of the form and do something on it.
This way, you can also do things like disabling the button if the form is invalid.
So here's what your form will look like:
import { Component } from '@angular/core';
import { FormGroup, FormBuilder, FormControl, Validators } from '@angular/forms';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
userForm: FormGroup;
constructor(private fb: FormBuilder) {}
ngOnInit() {
this.userForm = this.fb.group({
firstName: [, Validators.required],
lastName: [, Validators.required]
});
}
submitForm() {
console.log('Form Submitted with value: ', this.userForm.value);
}
}
And here's the template:
<h3>User Form</h3>
<hr>
<form [formGroup]="userForm">
<div>
<label for="firstName">First Name: </label>
<input type="text" id="firstName" formControlName="firstName">
</div>
<br>
<div>
<label for="lastName">Last Name: </label>
<input type="text" id="lastName" formControlName="lastName">
</div>
</form>
<br>
<button (click)="submitForm()" [disabled]="userForm.invalid">Submit</button>
Here's a Sample StackBlitz for your ref.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53331220%2fhow-can-i-associate-a-button-with-a-form-using-angular%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can achieve by reference
#form
of form and passing the same reference to button.
<button type='button' (click)="form.onsubmit()">
click me!
</button>
<form id='myform' #form action='#' onsubmit='console.log("wheee")'>
<input type='submit' value='me too'/>
</form>
Working copy is here - https://stackblitz.com/edit/angular-pnneks
add a comment |
You can achieve by reference
#form
of form and passing the same reference to button.
<button type='button' (click)="form.onsubmit()">
click me!
</button>
<form id='myform' #form action='#' onsubmit='console.log("wheee")'>
<input type='submit' value='me too'/>
</form>
Working copy is here - https://stackblitz.com/edit/angular-pnneks
add a comment |
You can achieve by reference
#form
of form and passing the same reference to button.
<button type='button' (click)="form.onsubmit()">
click me!
</button>
<form id='myform' #form action='#' onsubmit='console.log("wheee")'>
<input type='submit' value='me too'/>
</form>
Working copy is here - https://stackblitz.com/edit/angular-pnneks
You can achieve by reference
#form
of form and passing the same reference to button.
<button type='button' (click)="form.onsubmit()">
click me!
</button>
<form id='myform' #form action='#' onsubmit='console.log("wheee")'>
<input type='submit' value='me too'/>
</form>
Working copy is here - https://stackblitz.com/edit/angular-pnneks
answered Nov 16 '18 at 4:06
Sunil SinghSunil Singh
6,4472628
6,4472628
add a comment |
add a comment |
Tyr it like this.
<button type='submit' (click)="myForm.submit()">
click me!
</button>
<form id='myform' action='#' #myForm onsubmit='console.log("wheee")'>
<input type='submit' value='me too'/>
</form>
add a comment |
Tyr it like this.
<button type='submit' (click)="myForm.submit()">
click me!
</button>
<form id='myform' action='#' #myForm onsubmit='console.log("wheee")'>
<input type='submit' value='me too'/>
</form>
add a comment |
Tyr it like this.
<button type='submit' (click)="myForm.submit()">
click me!
</button>
<form id='myform' action='#' #myForm onsubmit='console.log("wheee")'>
<input type='submit' value='me too'/>
</form>
Tyr it like this.
<button type='submit' (click)="myForm.submit()">
click me!
</button>
<form id='myform' action='#' #myForm onsubmit='console.log("wheee")'>
<input type='submit' value='me too'/>
</form>
answered Nov 16 '18 at 4:06
Gabriel LopezGabriel Lopez
28217
28217
add a comment |
add a comment |
Well, you could use a Reactive Form and then on the click of this button. call a function that would extract the value of the form and do something on it.
This way, you can also do things like disabling the button if the form is invalid.
So here's what your form will look like:
import { Component } from '@angular/core';
import { FormGroup, FormBuilder, FormControl, Validators } from '@angular/forms';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
userForm: FormGroup;
constructor(private fb: FormBuilder) {}
ngOnInit() {
this.userForm = this.fb.group({
firstName: [, Validators.required],
lastName: [, Validators.required]
});
}
submitForm() {
console.log('Form Submitted with value: ', this.userForm.value);
}
}
And here's the template:
<h3>User Form</h3>
<hr>
<form [formGroup]="userForm">
<div>
<label for="firstName">First Name: </label>
<input type="text" id="firstName" formControlName="firstName">
</div>
<br>
<div>
<label for="lastName">Last Name: </label>
<input type="text" id="lastName" formControlName="lastName">
</div>
</form>
<br>
<button (click)="submitForm()" [disabled]="userForm.invalid">Submit</button>
Here's a Sample StackBlitz for your ref.
add a comment |
Well, you could use a Reactive Form and then on the click of this button. call a function that would extract the value of the form and do something on it.
This way, you can also do things like disabling the button if the form is invalid.
So here's what your form will look like:
import { Component } from '@angular/core';
import { FormGroup, FormBuilder, FormControl, Validators } from '@angular/forms';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
userForm: FormGroup;
constructor(private fb: FormBuilder) {}
ngOnInit() {
this.userForm = this.fb.group({
firstName: [, Validators.required],
lastName: [, Validators.required]
});
}
submitForm() {
console.log('Form Submitted with value: ', this.userForm.value);
}
}
And here's the template:
<h3>User Form</h3>
<hr>
<form [formGroup]="userForm">
<div>
<label for="firstName">First Name: </label>
<input type="text" id="firstName" formControlName="firstName">
</div>
<br>
<div>
<label for="lastName">Last Name: </label>
<input type="text" id="lastName" formControlName="lastName">
</div>
</form>
<br>
<button (click)="submitForm()" [disabled]="userForm.invalid">Submit</button>
Here's a Sample StackBlitz for your ref.
add a comment |
Well, you could use a Reactive Form and then on the click of this button. call a function that would extract the value of the form and do something on it.
This way, you can also do things like disabling the button if the form is invalid.
So here's what your form will look like:
import { Component } from '@angular/core';
import { FormGroup, FormBuilder, FormControl, Validators } from '@angular/forms';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
userForm: FormGroup;
constructor(private fb: FormBuilder) {}
ngOnInit() {
this.userForm = this.fb.group({
firstName: [, Validators.required],
lastName: [, Validators.required]
});
}
submitForm() {
console.log('Form Submitted with value: ', this.userForm.value);
}
}
And here's the template:
<h3>User Form</h3>
<hr>
<form [formGroup]="userForm">
<div>
<label for="firstName">First Name: </label>
<input type="text" id="firstName" formControlName="firstName">
</div>
<br>
<div>
<label for="lastName">Last Name: </label>
<input type="text" id="lastName" formControlName="lastName">
</div>
</form>
<br>
<button (click)="submitForm()" [disabled]="userForm.invalid">Submit</button>
Here's a Sample StackBlitz for your ref.
Well, you could use a Reactive Form and then on the click of this button. call a function that would extract the value of the form and do something on it.
This way, you can also do things like disabling the button if the form is invalid.
So here's what your form will look like:
import { Component } from '@angular/core';
import { FormGroup, FormBuilder, FormControl, Validators } from '@angular/forms';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
userForm: FormGroup;
constructor(private fb: FormBuilder) {}
ngOnInit() {
this.userForm = this.fb.group({
firstName: [, Validators.required],
lastName: [, Validators.required]
});
}
submitForm() {
console.log('Form Submitted with value: ', this.userForm.value);
}
}
And here's the template:
<h3>User Form</h3>
<hr>
<form [formGroup]="userForm">
<div>
<label for="firstName">First Name: </label>
<input type="text" id="firstName" formControlName="firstName">
</div>
<br>
<div>
<label for="lastName">Last Name: </label>
<input type="text" id="lastName" formControlName="lastName">
</div>
</form>
<br>
<button (click)="submitForm()" [disabled]="userForm.invalid">Submit</button>
Here's a Sample StackBlitz for your ref.
answered Nov 16 '18 at 4:59
SiddAjmeraSiddAjmera
16k31239
16k31239
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53331220%2fhow-can-i-associate-a-button-with-a-form-using-angular%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown