angular - ng-for 中的文本框仅在有数据时显示
问题描述
我正在研究角度。我在页面中有一些文本框,单击按钮时,我从数据库中选择数据并将其显示在文本框中。
在我显示数据之前,文本框不可见,我是角度新手,请帮助
ts中的代码:
addItems(value: any) {
this.items = new IComboDetails(value.ItemID, value.ItemCode, value.ItemDescription, value.PackingtypeID, value.PackingtypeName, value.quantity);
this.stockitems.push(this.items);
}
我的文本框代码:
<div class="col-md-12 col-sm-12 col-xs-12">
<div *ngFor="let match of stockitems">
<input type="text" class="form-control" placeholder="Item Code" required="" [(ngModel)]="match.ItemCode" value="{{match.ItemCode}}" />
</div>
</div>
按钮代码:
<input type="button" value="Add Item" class="btn btn-success" (click)="addItems(newStockoutForm.value);newStockoutForm.reset()" />
我不需要ngFor
循环,我只返回一个数据,但我不知道如何加载它。
解决方案
<ng-container *ngIf="!itemdetails || itemdetails.length == 0">
<label>Item Code</label>
<input type="text" class="form-control" name="temporaryField" />
</ng-container>
<div *ngIf="itemdetails && itemdetails.length > 0">
<ng-container *ngFor="let item of itemdetails">
<label>Item Code</label>
<input type="text" class="form-control" id="itemcode" name="itemcode" [value]="item.ItemCode" required />
</ng-container>
</div>
<br/><br/>
<button (click)="addItem()">Add Item</button>
</div>
`
})
推荐阅读
- javascript - 通过 chrome 扩展覆盖 window.confirm() 确认?
- arrays - 自动完成发送数据 id
- python - 在 Python 中使用“in”关键字在集合中查找项目时遇到问题
- ruby-on-rails - Select2 不加载数据
- julia - 朱莉娅的相同代码不同的结果
- angular - ng serve 不会编译
- javascript - 使用 Knex.js 和 Objection.js 连接到 Azure Postgres 数据库
- javascript - Babel 7 + Typescript 在函数方法中使用解构时出现意外标记
- xml - 在滚动视图中添加卡片视图时不起作用
- python - DBSCAN 轮廓系数:这个 for 循环有效吗?