我正在尝试将字符串“By - ”附加到通过 ngModel 从属性获取的值,即 <textarea [(ngModel)]="book.Author"..>
并将其添加到标题 <h2>{{book.Title}}</h2>
Angular
以便显示“标题作者 - 作者”。
数据是通过http api获取的。仅当文本区域中有作者值时,我才需要“作者 - 作者”。
我该如何实现这一目标?我是 Angular 新手。
最佳答案
解决方案可能是使用 Angular 指令 *ngIf
与 <ng-container>
结合在你的标题中,如下所示:
<h2>
{{book.Title}} <ng-container *ngIf="book.Author"> By - {{book.Author}}</ng-container>
</h2>
doesn't interfere with styles or layout because Angular doesn't put it in the DOM.
关于angular - 带有 [(ngModel)] 的硬编码字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52390564/