Instructions for opening a URL in a new tab using Angular

Within my Angular 10 application, I am conducting an API call that retrieves an external URL for downloading a pdf file. Is there a method to open this URL in a new browser tab without relying on the window object? I've been using window.open(url) successfully, but I'm curious if there are other alternatives available.

Answer №1

One suggestion is to include target="_blank" in the anchor tag (or button).

<a target="_blank" [routerLink]="['/link'] OR href">
    Open in New Window
</a>

Alternatively, have you considered using window.open? It may be a perfect solution for this situation.

Another option is to fetch the file through a service, convert it into a blob, and avoid opening a new window. Would that suit your needs?

public export(): Observable<any> {
    return this.http.get(`url/export`, { responseType: 'text' });
  }

Then,

public export(): Observable<any> {
    return this.apiService.export().pipe(tap(data => downloadAsCSVFile(data, 'test')));
}

const downloadAsCSVFile = (csv: string, fileName: string): void => {
  const blob = new Blob([csv], { type: 'text/csv' });
  saveAs(blob, `${fileName}.csv`);
};

You could give it a try!

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

Navigating the terrain of multiple checkboxes in React and gathering all the checked boxes

I am currently developing a filter component that allows for the selection of multiple checkboxes. My goal is to toggle the state of the checkboxes, store the checked ones in an array, and remove any unchecked checkboxes from the array. Despite my attemp ...

The error message "better-sqlite3 TypeError: o.default is not a constructor" indicates that

As part of my vscode extension development in typescript, webpack, and better-sqlite3, I am attempting to create a database within the C:\Users\userName\AppData\Roaming\Code\User\globalStorage\ folder. However, when ...

What is the best way to handle missing values in a web application using React and TypeScript?

When setting a value in a login form on the web and no value is present yet, should I use null, undefined, or ""? What is the best approach? In Swift, it's much simpler as there is only the option of nil for a missing value. How do I handle ...

Choosing the Angular5 model

I'm currently working with an Angular5 project that has a <select> element bound to an array of customers. Here's the code snippet: <select class="form-control" [ngModel]="record.customer_id" (ngModelChange)="setCustomer($event)" name=" ...

What is the process for integrating mobiscroll into an Angular project using a customized username?

After installing the mobiscroll trial in my angular project, I decided to reinstall it with a new username. Despite attempting to remove the library and using the command "mobiscroll config angular", I encountered an issue where it was still logging in w ...

Executing MongoDB collection operations with array filtering

I am looking to count records based on tags and filter them before including in specific groups // data in database {tags: ['video', 'Alex'], ... }, {tags: ['video', 'John'], ... }, {tags: ['video', 'J ...

The hidden pop-up window from a particular tool is currently not being displayed

I attempted to upload my code onto Stackblitz in search of assistance with my dynamic modal not displaying. I am working on a function that I intend to be able to call from any component to create a popup where I can dynamically modify the header, body, an ...

Using i18next to efficiently map an array of objects in TypeScript

I am currently converting a React project to TypeScript and using Packages like Next.js, next-i18next, and styled-components. EDIT: The information provided here may be outdated due to current versions of next-i18next. Please check out: Typescript i18ne ...

Unit testing for Angular service involving a mock Http GET request is essential for ensuring the

I am seeking guidance on how to test my service function that involves http get and post calls. I attempted to configure the spec file by creating an instance of the service, and also consulted several sources on creating a mockhttp service. However, I enc ...

When renaming a Vuex module, the system mistakenly believes the old module is still present

After attempting to rename my modules, I encountered a perplexing error. Originally, I had a module named "speed" which I decided to change to "team". Despite updating all references accordingly, I am now facing the following error message: Error: Missing ...

Guide on sorting an array within a specific range and extracting a sample on each side of the outcome

I need a simple solution for the following scenario: let rangeOfInterest = [25 , 44]; let input = [10, 20, 30, 40, 50, 60]; I want to extract values that fall between 25 and 44 (inclusive) from the given input. The range may be within or outside the inpu ...

Understanding TypeScript's ability to infer types in generics

Exploring the world of TypeScript through a robustly typed system for REST requests. Let's dive into the code: This type is used to establish the connection between routes and their respective object types: export interface RoutesMapping { api1: ...

Learning the implementation of uib-tabset in Angular 2 with ng-bootstrap

Currently, I am working on an angular2 project and I am curious to know if there is a way to utilize uib-tabset in angular2. In the past, when I was using angularjs, I could easily use ng-bootstrap. This allowed me to incorporate <uib-tabset></u ...

Setting up a Typescript project using webpack

Greetings! I am looking to set up Typescript with composite config and webpack (everything worked fine with just a single tsconfig.json). I must admit that I am new to TypeScript and have been more familiar with JavaScript. My main issue is getting the des ...

What is the best way to execute a function on the output of *ngFor directive in Angular 2?

Imagine having a list of all the users within your system: allUsers = { a: {name:'Adam',email:'<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="39585d5854794d5c4a4d5a56175a56... f: {name:'fred' ...

Refreshing an Angular page with parameterized routes on IIS results in a blank page

In an effort to resolve the 404 error that occurs when refreshing a page on my Angular application, I made modifications to the web.config file within the website folder on IIS. The code snippet added is shown below: <rule name="AngularJS Routes&qu ...

Utilizing React Native Camera Kit allows for the seamless and continuous scanning of QR codes, offering multiple callbacks when a code is

Attempting to integrate a QR code scanner into my project using react native. Utilizing the plugin react-native-camera-kit, which supports both QR and Bar code scanning. However, I am facing an issue where the scanner continuously scans the code and trig ...

The reason behind the ghost problem - classRef isn't recognized as a constructor

Recently, I encountered an issue where the app was not loading in the browser while running an Angular NX project locally with the command "start-dev": "nx run-many --target=serve --all". The screen would get stuck on our loading animat ...

Transmit HTTP headers when making an HTTP request, similar to using Postman

Encountered an issue with Angular while attempting to send an HTTP POST request, resulting in the following error: Http failure during parsing for service.ts currentUse(SVF){ //let options = {headers: new HttpHeaders({'Content-Type': &apos ...

The main module's postinstall process is initiated before the sub-module's postinstall process

Update: I am seeking guidance on how to install a module from GitHub instead of npm. That's the main query. In case you're wondering why: I'm currently working on some confidential projects and prefer not to publish the code. As a result, ...