Posting an Angular 6 form to open in a separate tab

I have been exploring ways to set a target on a form within Angular. My goal is to have the submission of the form occur in a new tab, so I tried using the code below:

<form id="formLanding" role="form" (ngSubmit)="onSubmit()" #landingForm="ngForm" [target]="'_blank'">
...
</form>

Unfortunately, this approach does not post the form to a new tab as expected. I came across information about noNgForm, but it did not work for my specific case. The directive removes the form reference, which conflicts with the extensive work I have already done that integrates with Angular's form validation. Does anyone know of a way to successfully submit a form to a new tab while still utilizing Angular forms?

Answer №1

While it may not be the most visually appealing solution, I had to find a way to meet this requirement that was non-negotiable. Here is how I tackled it.

The workaround involved creating another form element that would open a new page with a get request (since pop up blockers don't interfere with form loads).

<form  ngNoForm #htmlForm hidden action="{{window?.location?.href}}" method="get" target="_blank">
  <input name="zip" value="{{formData.zipCode}}"/>
  <input name="autoSubmit" value="true"/>
</form>

By doing this, it opens the current page in a new tab with the form values as query parameters. Then, within your component, you can access the form reference.

@ViewChild("htmlForm")
htmlForm: ElementRef;

In the click handler for the angular managed form, you can implement any necessary logic along with

this.htmlForm.nativeElement.submit();

Additionally, upon initialization of the component, you can retrieve these form values and automatically submit them.

ngOnInit() {
    this.activatedRoute.queryParams.subscribe(params => {
      if (params && params.autoSubmit) {

        this.formData.zipCode = params.zip;
        //submit this.formData to the server. 
      }
    });
  }

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Obtain the Angular 2 Form object within a TypeScript class file for manual configuration of form properties

I have a form inside a PrimeNG dialog control. I need to handle two different submit methods based on certain conditions for this form, so I don't want to use the ngSubmit method in the form tag. Instead, I want to manually access the form object in m ...

Struggling with TypeScript errors when using Vue in combination with Parcel?

While running a demo using vue + TypeScript with Parcel, I encountered an error in the browser after successfully bootstrapping: vue.runtime.esm.js:7878 Uncaught TypeError: Cannot read property 'split' of undefined at Object.exports.install ...

Unlock the power of Angular ViewChildren to access and manipulate SVG elements efficiently

I have an SVG file loaded as an object: <object data="assets/img/states.svg" type="image/svg+xml" id="map"></object> This SVG includes a large PNG map along with several rect and text elements. <rect y="224.72084" x="644.87109" ...

What could be causing Next.js to re-render the entire page unnecessarily?

As a newcomer to Next.js, I am trying to develop an app where the header/navbar remains fixed at all times. Essentially, when the user navigates to different pages, only the main content should update without refreshing the navbar. Below is the code I have ...

Manipulating array objects by replacing values in Typescript

Attempted two different methods to obtain a partial summary within each array object, but unfortunately, both were unsuccessful. var arr = [ { "value": 10, "newBalance": 0 }, { "value": -10, "newBalance": 0 }, ...

Having trouble with the rowNode functionality in PrimeNG TreeTable

I am currently utilizing the PrimeNG Treetable component from https://www.primefaces.org/primeng/#/treetable I seem to be encountering issues with retrieving data from the service. Below is a snippet of my code: HTML <p-treeTable [value]="temp"> & ...

Is it possible to navigate to a particular step during an Angular IntroJS event?

Whenever I attempt to navigate to a specific step from within the IntroJS onexit event, it seems to cause confusion and display the incorrect step along with the highlighted element. I have defined my steps using JSON, with a total of 8 steps: [{ elem ...

Issue with sx prop type error found in the TabPanel component of MUI v5

My first time using MUI with TypeScript has hit a roadblock with the new sx prop. Check out the error displayed by TypeScript in the screenshot linked below: https://i.sstatic.net/JYDTX.png Interestingly, the error only pops up on the TabPanel Component, ...

The 'GoogleAuthProvider' property cannot be found on the 'AngularFireAuth' type

When attempting to sign in with Google using 'AngularFireAuth', I encountered an error. Here is the code snippet from my auth.service.ts file: import { Injectable } from '@angular/core'; import { first } from 'rxjs/operators'; ...

Try utilizing const instead of let to avoid unexpected changes

Encountered an issue with the no-let rule in my tslint configuration. It is flagging the handler variable as requiring a const declaration, despite the fact that it is assigned within a switch case statement. This seems like a potential bug to me. static ...

Guide on utilizing a module to define numerous elements in Angular 6

I am trying to use the joke.module.ts module to specify multiple components. Starting with JokeComponent in joke/joke.module.ts within the src/app directory. import { Component } from '@angular/core';/ @Component({ selector: 'joke', ...

I'm struggling to see the practical application of the angular signal effect function

What is the purpose of the effect function in Angular signals? The effect function does not return anything and typically cannot update other signals by default (although this can be disabled). So, in what scenarios is it actually beneficial? What is the ...

Validating PrimeNG Tables

I have a PrimeNG Table with only one editable column called "Value". Here is the StackBlitz demo for reference. https://stackblitz.com/edit/datatablevalidation In the "Value" column, I need to implement validation based on the value in the corresponding ...

Angular 6's inability to subscribe to a subject from multiple components simultaneously is causing issues

Having an issue with data subscription between parent and child components and a service. When emitting data using the next method in the subject class, only the child component receives the data. In navbar.component.ts: this.messageService.setClientUser ...

modify the background color at core.scss line 149

I am struggling to change the background color of my Ionic login page. I have tried adding custom CSS in various places, such as core.scss:149, but it only works when I add it directly in Chrome developer tools. How can I get this custom background color t ...

List of duplicated BLE devices detected in network scanning

Greetings! I am currently working on an Ionic project named BLE Scanner. After facing some challenges, I finally managed to connect to the devices. Below is the code snippet that I discovered online: home.ts (please ignore the DetailPage) import { Compon ...

Creating a versatile protractor framework to streamline multiple project implementations

There's a concept in the works for creating a 'protractor core' that will be utilized by various projects for UI testing. Currently, I have an Angular project called 'project1' with e2e tests (cucumber-protractor-typescript) that c ...

The never-ending scroll feature in Vue.js

For the component of cards in my project, I am trying to implement infinite scrolling with 3 cards per row. Upon reaching the end of the page, I intend to make an API call for the next page and display the subsequent set of cards. Below is my implementatio ...

Can users define their own customized shortcut keys in the web application?

I'm working on a web application built with Angular 7.0 and Node.js. I'm looking to implement a shortcut engine similar to Microsoft Word, where users can customize or modify default shortcut keys for certain functions in the app. Are there any c ...

Tips for incorporating recursive HTTP requests into an Angular2 service to efficiently retrieve data in advance

In my Angular project, I am using a service to fetch data from an external API. However, the API has a limit of 100 records per request and I can only determine the total number of records available after fetching the first batch. The response structure o ...