admin管理员组文章数量:1389749
I am sure I am missing something simple here. I have a very simple component with one form with a submit button. I am unable to get the onsubmit handler to work correctly in Safari, but it works fine in Chrome.
Component HTML
<div>
<form [formGroup]="linkFormGroup" (ngSubmit)="submitPost()">
<label for="url">URL</label>
<input type=text size="80" id="url" formControlName="url">
<label for="comment">Post</label>
<textarea id="comment" formControlName="comment"></textarea>
<button type="submit">Submit</button>
</form>
</div>
Component Typescript code:
import { Component } from '@angular/core';
import { FormsModule, FormGroup, FormControl, ReactiveFormsModule } from '@angular/forms';
import { AsyncPipe } from '@angular/common';
import { Observable } from 'rxjs';
@Component({
imports: [FormsModule, AsyncPipe, ReactiveFormsModule],
selector: 'post-link',
templateUrl: './post-linkponent.html',
styleUrl: './post-linkponent.css'
})
export class PostLinkComponent {
post_content: string="";
linkMetadata$! : Observable<LinkMetadata>;
linkFormGroup = new FormGroup({
url: new FormControl(''),
comment: new FormControl(''),
})
submitPost() {
console.log(this.linkFormGroup.value);
}
}
When I click Submit in Safari, nothing happens. When I click in Chrome, I see the appropriate logging in the console. Any thoughts? Thanks.
I am sure I am missing something simple here. I have a very simple component with one form with a submit button. I am unable to get the onsubmit handler to work correctly in Safari, but it works fine in Chrome.
Component HTML
<div>
<form [formGroup]="linkFormGroup" (ngSubmit)="submitPost()">
<label for="url">URL</label>
<input type=text size="80" id="url" formControlName="url">
<label for="comment">Post</label>
<textarea id="comment" formControlName="comment"></textarea>
<button type="submit">Submit</button>
</form>
</div>
Component Typescript code:
import { Component } from '@angular/core';
import { FormsModule, FormGroup, FormControl, ReactiveFormsModule } from '@angular/forms';
import { AsyncPipe } from '@angular/common';
import { Observable } from 'rxjs';
@Component({
imports: [FormsModule, AsyncPipe, ReactiveFormsModule],
selector: 'post-link',
templateUrl: './post-linkponent.html',
styleUrl: './post-linkponent.css'
})
export class PostLinkComponent {
post_content: string="";
linkMetadata$! : Observable<LinkMetadata>;
linkFormGroup = new FormGroup({
url: new FormControl(''),
comment: new FormControl(''),
})
submitPost() {
console.log(this.linkFormGroup.value);
}
}
When I click Submit in Safari, nothing happens. When I click in Chrome, I see the appropriate logging in the console. Any thoughts? Thanks.
Share Improve this question asked Mar 16 at 17:31 nealneal 5672 silver badges9 bronze badges1 Answer
Reset to default 0- Safari has strict restrictions with button submissions I think.
- so I added a event to the submit button and basically am effectively preventing the default behavior on click
<div class="form-container">
<form [formGroup]="linkFormGroup" (ngSubmit)="submitPost($event)">
<div>
<label for="url">URL</label>
<input type="text" size="80" id="url" formControlName="url">
</div>
<div>
<label for="comment">Post</label>
<textarea id="comment" formControlName="comment"></textarea>
</div>
<button type="submit">Submit</button>
</form>
</div>
submitPost(event: Event) {
event.preventDefault();
const formValues = this.linkFormGroup.value;
alert(`Form submitted with values:\nURL: ${formValues.url}\nComment: ${formValuesment}`);
console.log(formValues);
}
本文标签: Angular form submit button not working in SafariStack Overflow
版权声明:本文标题:Angular form submit button not working in Safari - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744591462a2614515.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论