Creating dynamic HTML content for printing tasks with the use of variables in Angular6

After running this code, I noticed that instead of values, I am receiving the object names.

export class PrescriberComponent implements OnInit {
constructor() { }

people = [
    {id: 1, forename: 'John', surname: 'Doe'},
    {id: 2, forename: 'John', surname: 'Smith'},
    {id: 3, forename: 'Peter', surname: 'Scott'},
    {id: 4, forename: 'Sue', surname: 'Reece'}
];

PrintDoc1() : void {  
    var originalContents = ` 
    <table border="3" width="50%" cellpadding="2" cellspacing="3">
    <tr>
        <th colspan="2">
            <h2>table title</h2>
        </th>
    </tr>
    <tr>
        <th>First Name</th>
    </tr>
    <tr *ngFor="person in this.people">
        <td>{{person.forename}}</td>
    </tr>
    </table>
    `;  
    console.log(originalContents);
     var popupWin = window.open('', '_blank', 'width=600,height=600,scrollbars=no,menubar=no,toolbar=no,location=center,status=no,titlebar=no');
     popupWin.window.focus();
     popupWin.document.write('<!DOCTYPE html><html><head>' +
         '<link rel="stylesheet" type="text/css" href="style.css" />' +
         '</head><body onload="window.print()"><div class="reward-body">' + originalContents + '</div></html>');
    popupWin.document.close();
}

}



<button type="button" style="color: #ffffff;background-color: #28afde" (click)="PrintDoc1()">PRINT</button>

When analyzing the output, it appears that only the object names are displayed instead of the actual values. I suspect that the issue lies with *ngFor and interpolation not functioning correctly in this context.

Answer №1

Experiment with ngFor by following this example and test it for yourself

<ul *ngFor="let individual of individuals">
  {{ individual.name}}
</ul>

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

Receiving a Typescript error stating "Property '0' does not exist on type X" when incorporating auto-generated GraphQL code into the project

I'm struggling to comprehend how to rectify this typographical error. Here's the snippet of code causing me trouble: <script lang="ts"> import type { PlayerListQuery } from "queries"; export let player: PlayerListQ ...

Angular 6: Inconsistent performance of [(ngModel)] functionality

In my Angular 6 project, I am utilizing Bootstrap Modals to implement a specific functionality. Upon clicking the modify button, a modal window should appear with pre-filled values. https://i.sstatic.net/PIg0L.jpg Although I am using template-driven form ...

Tips for optimizing the performance of an Angular 2 website

I am currently developing a website in Angular 2 and have successfully completed one module which I uploaded to the demo path. However, when I tested it using Google Speed Test, it only received a speed score of 37 on desktop and 32 on mobile. Can anyone ...

Is it necessary to also include template-only attributes in my controller's definition?

I'm working on a directive template that features a basic toggle variable. <div ng-mouseenter="$ctrl.myToggle = true" ng-mouseleave="$ctrl.myToggle = false"> ... </div> <div ng-if="$ctrl.myToggle"> ... toggled content </div> ...

arrange items based on their category into arrays

I have a JSON file that needs to be arranged based on three specific fields. Here is an example snippet of the JSON data: { "Racename": "10KM", "Category": 34, "Gender": "Male", "Work": "Google", "FullName": "Dave Happner", "Rank": 1, "Poni ...

Modifying the State in a Class Component

private readonly maxSizeOfDownloadedFiles: number = 1000000; state = { totalSum: this.maxSizeOfDownloadedFiles }; handleCallback = () => { this.setState({ totalSum: 12 }) alert('totalSum ' + this.state.totalSum); }; Upon executing the ...

Navigate to a different route in AntD Table by clicking on it

I am currently implementing React Router in my navigation component. My goal is to enable users to navigate from screen Y to screen X when they click on a table element. In the past, I achieved this by using "this" command but it seems that it does not ...

Angular2, RxJS Subject HTTP request - If an error occurs, the request will not be triggered again

Encountering an issue with handling an http error response (specifically a 422 validation error from the backend). After receiving an http error response, unable to trigger the request again by clicking the button. 1) Utilizing a subject property and cal ...

Can anyone suggest a more efficient method for specifying the type of a collection of react components?

Picture this scenario: you are extracting data from an API and creating a list of Card components to be displayed in a parent component. Your code might resemble the following: function App() { let items = [] // How can I specify the type here to avoid ...

Incorporating ngrx/Store into a current Angular application

Currently, I am working on an Angular 7 project that consists of numerous components communicating with an API to update data. The constant refreshing of the data using setTimeout has made it quite overwhelming as all the components are pulling data from t ...

Encountering a "Cannot POST" error in Angular upon redirection to a callback URL with a SAML response

Currently, I'm implementing SAML authentication in my angular application that will be hosted on AWS. The angular code is stored in a separate project from the webapi where Itfoxtec saml library is being utilized. The flow of communication between my ...

What leads to the inability to utilize environment variables in this TypeScript app built with Vue 3?

Currently, I am developing a single page application utilizing Vue 3 and TypeScript. The main purpose of this app is to interact with an API. All the necessary information including the API's URL and key are stored in the 'src\env.js' f ...

Creating reversed/generate URLs from Routes in Angular2 (using typescript)

Is there a way to automatically create a URL from a route using code? For instance, let's say I have a login component defined in my routes: const appRoutes: Routes = [ ... { path: 'login', component: LoginComponent }, ... ]; N ...

How come the useEffect hook is causing re-rendering on every page instead of just the desired endpoint?

I am a novice attempting to retrieve data from a database and display it on the front-end using Axios, ReactJS, and TypeScript. My intention is for the data to be rendered specifically on localhost:3000/api/v1/getAll, but currently, it is rendering on all ...

What is the correct version compatibility matrix for Expo, NPM, Node, React Native, and TypeScript?

Currently, I am in the process of setting up React Native with TypeScript. Here are the steps I followed: npx react-native init MyApp --template react-native-template-typescript I made sure to install TypeScript as well: npm install -g typescript ' ...

Asynchronous data fetching adding two entries to an array

I've been experimenting with making API calls for Rick & Morty using fetch and async arrow functions, but I've run into an issue where the elements are being added to my array twice. I've tried calling the function both with and without useE ...

Is it possible to configure a custom timezone for the Angular Material datepicker feature?

I am currently working on an Angular 7 application and I have encountered a challenge with the date field. In this particular field, I am utilizing the Angular Material Datepicker input. However, I have noticed that the datepicker automatically captures th ...

Searching for values within an array of objects by iterating through nested arrays to apply a filter

Having trouble returning the testcaseid from an array to this.filteredArray Able to fetch header value and all values of the array when the search word is empty. Seeking assistance with iterating through the testcaseid and header on the search input fiel ...

Incorporate dynamic HTML snippets into the DOM with Angular 4

Looking to populate an unordered list element <ul> with small, straightforward snippets from my JavaScript. Just basic lines like <li>Label: Text</li>. Using the ViewContainerRef.createComponent(ItemComponent) method to create a new comp ...

What is the best way to include an item in a list with a specific identifier using the useState Record data type in a React application built with TypeScript?

Here is the structure of my Record type: const PEOPLE_MAP_INIT: Record<string, Person[]> = { "1": [], "2": [], "3": [] }; I have initialized the useState like this: const [PEOPLE_MAP, SET_PEO ...