按 POST 请求提交表格并在 Angular 中打开新的 tab/window
Submit form as POST request and open new tab/window in Angular
我正在使用 Angular,我想提交一个表单作为 POST 请求并在新的 window 或选项卡中打开结果。我需要在 Authorization header 或 POST body 中发送身份验证令牌,而不是作为 GET 查询参数。项目要求不对此请求使用 AJAX。
我已经在 Angular 根组件之外的 index.html
中尝试了一个表单:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Angular</title>
<base href="/" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="icon" type="image/x-icon" href="favicon.ico" />
<link rel="preconnect" href="https://fonts.gstatic.com" />
<link href="https://fonts.googleapis.com/css2?family=Roboto:wght@300;400;500&display=swap" rel="stylesheet" />
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet" />
</head>
<body class="mat-typography">
<form method="post" target="_blank" action="https://www.google.com/">
<input type="hidden" value="auth-token">
<button mat-button color="primary" type="submit">
Submit
</button>
<input type="submit" value="Submit" />
</form>
<app-root></app-root>
</body>
</html>
这按预期工作。表单已发送,结果显示在新选项卡中。但是我需要在 mat-table
.
内的 ngFor
内使用此功能
当我将此表单移动到根 Angular 组件时
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
template: `
<form method="post" target="_blank" action="https://www.google.com/">
<input type="hidden" value="auth-token">
<button mat-button color="primary" type="submit">
Submit
</button>
<input type="submit" value="Submit" />
</form>`,
styleUrls: ['./app.component.scss']
})
export class AppComponent {}
我可以看到表单和按钮,但是单击既不会发送表单也不会打开新选项卡。
我已经在项目中导入了 FormsModule
和 use/need 模块。我想,该模块阻止了表单的默认行为。是否可以在导入 FormsModule
的 Angular 项目中强制提交表单的默认行为?
我检查了 https://angular.io/api/forms/NgForm 但找不到允许默认行为的选项。我还尝试导入 ReactiveFormsModule
.
这 Stackblitz 说明了这个问题。上面两个按钮在Angular组件外面,打开一个新的tab/window。下方的两个按钮位于 Angular 组件内,不执行任何操作。
由于有人因缺少调试细节而投票关闭,以下是 Stackblitz 的文件:
index.html:
<form method="post" target="_blank" action="https://www.google.com/">
<input type="hidden" value="auth-token" />
<button mat-button color="primary" type="submit">Submit</button>
<input type="submit" value="Submit" />
</form>
<my-app>loading</my-app>
app.module.ts:
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { AppComponent } from './app.component';
@NgModule({
imports: [ BrowserModule, FormsModule ],
declarations: [ AppComponent ],
bootstrap: [ AppComponent ]
})
export class AppModule { }
app.component.ts:
import { Component, VERSION } from '@angular/core';
@Component({
selector: 'my-app',
templateUrl: './app.component.html'
})
export class AppComponent {
name = 'Angular ' + VERSION.major;
}
app.component.ts:
<form method="post" target="_blank" action="https://www.google.com/">
<input type="hidden" value="auth-token" />
<button mat-button color="primary" type="submit">Submit</button>
<input type="submit" value="Submit" />
</form>
main.ts:
import './polyfills';
import { enableProdMode } from '@angular/core';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app/app.module';
platformBrowserDynamic().bootstrapModule(AppModule).then(ref => {
// Ensure Angular destroys itself on hot reloads.
if (window['ngRef']) {
window['ngRef'].destroy();
}
window['ngRef'] = ref;
// Otherwise, log the boot error
}).catch(err => console.error(err));
polyfill.ts:
import 'zone.js/dist/zone';
angular.json:
{
"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
"version": 1,
"newProjectRoot": "projects",
"projects": {
"demo": {
"root": "",
"sourceRoot": "src",
"projectType": "application",
"prefix": "app",
"schematics": {},
"architect": {
"build": {
"builder": "@angular-devkit/build-angular:browser",
"options": {
"outputPath": "dist/demo",
"index": "src/index.html",
"main": "src/main.ts",
"polyfills": "src/polyfills.ts",
"tsConfig": "src/tsconfig.app.json",
"assets": [
"src/favicon.ico",
"src/assets"
],
"styles": [
],
"scripts": []
},
"configurations": {
"production": {
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.prod.ts"
}
],
"optimization": true,
"outputHashing": "all",
"sourceMap": false,
"extractCss": true,
"namedChunks": false,
"aot": true,
"extractLicenses": true,
"vendorChunk": false,
"buildOptimizer": true
}
}
},
"serve": {
"builder": "@angular-devkit/build-angular:dev-server",
"options": {
"browserTarget": "demo:build"
},
"configurations": {
"production": {
"browserTarget": "demo:build:production"
}
}
},
"extract-i18n": {
"builder": "@angular-devkit/build-angular:extract-i18n",
"options": {
"browserTarget": "demo:build"
}
},
"test": {
"builder": "@angular-devkit/build-angular:karma",
"options": {
"main": "src/test.ts",
"polyfills": "src/polyfills.ts",
"tsConfig": "src/tsconfig.spec.json",
"karmaConfig": "src/karma.conf.js",
"styles": [
"styles.css"
],
"scripts": [],
"assets": [
"src/favicon.ico",
"src/assets"
]
}
},
"lint": {
"builder": "@angular-devkit/build-angular:tslint",
"options": {
"tsConfig": [
"src/tsconfig.app.json",
"src/tsconfig.spec.json"
],
"exclude": [
"**/node_modules/**"
]
}
}
}
}
},
"defaultProject": "demo"
}
package.json:
{
"name": "angular",
"version": "0.0.0",
"private": true,
"dependencies": {
"@angular/animations": "^13.2.0",
"@angular/common": "^13.2.0",
"@angular/compiler": "^13.2.0",
"@angular/core": "^13.2.0",
"@angular/forms": "^13.2.0",
"@angular/platform-browser": "^13.2.0",
"@angular/platform-browser-dynamic": "^13.2.0",
"@angular/router": "^13.2.0",
"rxjs": "^7.5.2",
"tslib": "^2.3.1",
"zone.js": "^0.11.4"
},
"scripts": {
"ng": "ng",
"start": "ng serve",
"build": "ng build",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e"
},
"devDependencies": {
"@angular-devkit/build-angular": "~0.1100.4",
"@angular/cli": "~11.0.4",
"@angular/compiler-cli": "~11.0.4",
"@types/jasmine": "~3.6.0",
"@types/node": "^12.11.1",
"codelyzer": "^6.0.0",
"jasmine-core": "~3.6.0",
"jasmine-spec-reporter": "~5.0.0",
"karma": "~5.1.0",
"karma-chrome-launcher": "~3.1.0",
"karma-coverage": "~2.0.3",
"karma-jasmine": "~4.0.0",
"karma-jasmine-html-reporter": "^1.5.0",
"protractor": "~7.0.0",
"ts-node": "~8.3.0",
"tslint": "~6.1.0",
"typescript": "~4.0.2"
}
}
tsconfig.json:
{
"compileOnSave": false,
"compilerOptions": {
"baseUrl": "./",
"outDir": "./dist/out-tsc",
"sourceMap": true,
"declaration": false,
"downlevelIteration": true,
"experimentalDecorators": true,
"module": "esnext",
"moduleResolution": "node",
"importHelpers": true,
"target": "es2015",
"typeRoots": [
"node_modules/@types"
],
"lib": [
"es2018",
"dom"
]
},
"angularCompilerOptions": {
"enableIvy": true,
"fullTemplateTypeCheck": true,
"strictInjectionParameters": true
}
}
在您的 App.module.ts 文件中导入 ReactiveFormsModule
更新:
这样试试:
<form #form method="post" target="_blank" action="https://www.google.com/">
<input type="hidden" value="auth-token" />
<button mat-button color="primary" type="submit" (click)="form.submit()">Submit</button>
<input type="submit" value="Submit" />
</form>
angular 表单模块拦截表单元素提交事件。您可以绑定到 ngSubmit
并使用元素引用提交表单,提交方法/按钮都适用于此方法:
<form
#authForm
method="post"
target="_blank"
action="https://www.google.com/"
(ngSubmit)="authForm.submit()"
>
<input type="hidden" value="auth-token" />
<button mat-button color="primary" type="submit">Submit</button>
<input type="submit" value="Submit" />
</form>
我正在使用 Angular,我想提交一个表单作为 POST 请求并在新的 window 或选项卡中打开结果。我需要在 Authorization header 或 POST body 中发送身份验证令牌,而不是作为 GET 查询参数。项目要求不对此请求使用 AJAX。
我已经在 Angular 根组件之外的 index.html
中尝试了一个表单:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Angular</title>
<base href="/" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="icon" type="image/x-icon" href="favicon.ico" />
<link rel="preconnect" href="https://fonts.gstatic.com" />
<link href="https://fonts.googleapis.com/css2?family=Roboto:wght@300;400;500&display=swap" rel="stylesheet" />
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet" />
</head>
<body class="mat-typography">
<form method="post" target="_blank" action="https://www.google.com/">
<input type="hidden" value="auth-token">
<button mat-button color="primary" type="submit">
Submit
</button>
<input type="submit" value="Submit" />
</form>
<app-root></app-root>
</body>
</html>
这按预期工作。表单已发送,结果显示在新选项卡中。但是我需要在 mat-table
.
ngFor
内使用此功能
当我将此表单移动到根 Angular 组件时
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
template: `
<form method="post" target="_blank" action="https://www.google.com/">
<input type="hidden" value="auth-token">
<button mat-button color="primary" type="submit">
Submit
</button>
<input type="submit" value="Submit" />
</form>`,
styleUrls: ['./app.component.scss']
})
export class AppComponent {}
我可以看到表单和按钮,但是单击既不会发送表单也不会打开新选项卡。
我已经在项目中导入了 FormsModule
和 use/need 模块。我想,该模块阻止了表单的默认行为。是否可以在导入 FormsModule
的 Angular 项目中强制提交表单的默认行为?
我检查了 https://angular.io/api/forms/NgForm 但找不到允许默认行为的选项。我还尝试导入 ReactiveFormsModule
.
这 Stackblitz 说明了这个问题。上面两个按钮在Angular组件外面,打开一个新的tab/window。下方的两个按钮位于 Angular 组件内,不执行任何操作。
由于有人因缺少调试细节而投票关闭,以下是 Stackblitz 的文件:
index.html:
<form method="post" target="_blank" action="https://www.google.com/">
<input type="hidden" value="auth-token" />
<button mat-button color="primary" type="submit">Submit</button>
<input type="submit" value="Submit" />
</form>
<my-app>loading</my-app>
app.module.ts:
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { AppComponent } from './app.component';
@NgModule({
imports: [ BrowserModule, FormsModule ],
declarations: [ AppComponent ],
bootstrap: [ AppComponent ]
})
export class AppModule { }
app.component.ts:
import { Component, VERSION } from '@angular/core';
@Component({
selector: 'my-app',
templateUrl: './app.component.html'
})
export class AppComponent {
name = 'Angular ' + VERSION.major;
}
app.component.ts:
<form method="post" target="_blank" action="https://www.google.com/">
<input type="hidden" value="auth-token" />
<button mat-button color="primary" type="submit">Submit</button>
<input type="submit" value="Submit" />
</form>
main.ts:
import './polyfills';
import { enableProdMode } from '@angular/core';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app/app.module';
platformBrowserDynamic().bootstrapModule(AppModule).then(ref => {
// Ensure Angular destroys itself on hot reloads.
if (window['ngRef']) {
window['ngRef'].destroy();
}
window['ngRef'] = ref;
// Otherwise, log the boot error
}).catch(err => console.error(err));
polyfill.ts:
import 'zone.js/dist/zone';
angular.json:
{
"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
"version": 1,
"newProjectRoot": "projects",
"projects": {
"demo": {
"root": "",
"sourceRoot": "src",
"projectType": "application",
"prefix": "app",
"schematics": {},
"architect": {
"build": {
"builder": "@angular-devkit/build-angular:browser",
"options": {
"outputPath": "dist/demo",
"index": "src/index.html",
"main": "src/main.ts",
"polyfills": "src/polyfills.ts",
"tsConfig": "src/tsconfig.app.json",
"assets": [
"src/favicon.ico",
"src/assets"
],
"styles": [
],
"scripts": []
},
"configurations": {
"production": {
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.prod.ts"
}
],
"optimization": true,
"outputHashing": "all",
"sourceMap": false,
"extractCss": true,
"namedChunks": false,
"aot": true,
"extractLicenses": true,
"vendorChunk": false,
"buildOptimizer": true
}
}
},
"serve": {
"builder": "@angular-devkit/build-angular:dev-server",
"options": {
"browserTarget": "demo:build"
},
"configurations": {
"production": {
"browserTarget": "demo:build:production"
}
}
},
"extract-i18n": {
"builder": "@angular-devkit/build-angular:extract-i18n",
"options": {
"browserTarget": "demo:build"
}
},
"test": {
"builder": "@angular-devkit/build-angular:karma",
"options": {
"main": "src/test.ts",
"polyfills": "src/polyfills.ts",
"tsConfig": "src/tsconfig.spec.json",
"karmaConfig": "src/karma.conf.js",
"styles": [
"styles.css"
],
"scripts": [],
"assets": [
"src/favicon.ico",
"src/assets"
]
}
},
"lint": {
"builder": "@angular-devkit/build-angular:tslint",
"options": {
"tsConfig": [
"src/tsconfig.app.json",
"src/tsconfig.spec.json"
],
"exclude": [
"**/node_modules/**"
]
}
}
}
}
},
"defaultProject": "demo"
}
package.json:
{
"name": "angular",
"version": "0.0.0",
"private": true,
"dependencies": {
"@angular/animations": "^13.2.0",
"@angular/common": "^13.2.0",
"@angular/compiler": "^13.2.0",
"@angular/core": "^13.2.0",
"@angular/forms": "^13.2.0",
"@angular/platform-browser": "^13.2.0",
"@angular/platform-browser-dynamic": "^13.2.0",
"@angular/router": "^13.2.0",
"rxjs": "^7.5.2",
"tslib": "^2.3.1",
"zone.js": "^0.11.4"
},
"scripts": {
"ng": "ng",
"start": "ng serve",
"build": "ng build",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e"
},
"devDependencies": {
"@angular-devkit/build-angular": "~0.1100.4",
"@angular/cli": "~11.0.4",
"@angular/compiler-cli": "~11.0.4",
"@types/jasmine": "~3.6.0",
"@types/node": "^12.11.1",
"codelyzer": "^6.0.0",
"jasmine-core": "~3.6.0",
"jasmine-spec-reporter": "~5.0.0",
"karma": "~5.1.0",
"karma-chrome-launcher": "~3.1.0",
"karma-coverage": "~2.0.3",
"karma-jasmine": "~4.0.0",
"karma-jasmine-html-reporter": "^1.5.0",
"protractor": "~7.0.0",
"ts-node": "~8.3.0",
"tslint": "~6.1.0",
"typescript": "~4.0.2"
}
}
tsconfig.json:
{
"compileOnSave": false,
"compilerOptions": {
"baseUrl": "./",
"outDir": "./dist/out-tsc",
"sourceMap": true,
"declaration": false,
"downlevelIteration": true,
"experimentalDecorators": true,
"module": "esnext",
"moduleResolution": "node",
"importHelpers": true,
"target": "es2015",
"typeRoots": [
"node_modules/@types"
],
"lib": [
"es2018",
"dom"
]
},
"angularCompilerOptions": {
"enableIvy": true,
"fullTemplateTypeCheck": true,
"strictInjectionParameters": true
}
}
在您的 App.module.ts 文件中导入 ReactiveFormsModule
更新:
这样试试:
<form #form method="post" target="_blank" action="https://www.google.com/">
<input type="hidden" value="auth-token" />
<button mat-button color="primary" type="submit" (click)="form.submit()">Submit</button>
<input type="submit" value="Submit" />
</form>
angular 表单模块拦截表单元素提交事件。您可以绑定到 ngSubmit
并使用元素引用提交表单,提交方法/按钮都适用于此方法:
<form
#authForm
method="post"
target="_blank"
action="https://www.google.com/"
(ngSubmit)="authForm.submit()"
>
<input type="hidden" value="auth-token" />
<button mat-button color="primary" type="submit">Submit</button>
<input type="submit" value="Submit" />
</form>