使用Angular6中的变量为打印任务动态生成HTML



当我运行此代码时,我不会获取值,而是获取对象名称。

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>

在输出中,我们只能看到对象名称,而不能看到值。我怀疑*ngFor和插值不是这样工作的。

尝试使用ngFor,就像这样,并尝试

<li *ngFor="let person of people">
{{ person.forename}}
</li>

最新更新