[英]Angular 6 data not getting binded to UI OnInit
I have a problem that my dynamic data is not getting binded to the UI and also Data tables.我有一个问题,我的动态数据没有绑定到 UI 和数据表。 I have tried using various ways but its not working.
我尝试过使用各种方法,但它不起作用。 I am using smart admin latest theme for my development of website
我正在使用 smart admin 最新主题来开发网站
when I hit the api i get response.当我点击 api 时,我得到了回应。
var tabledata:any[]=[]
Get(){
this.getservice().subscribe(
res=>{
if(res && res.data && res.data.length>0){
this.tabledata=res.data;
console.log(this.tabledata);
}
}
)
}
In html在 html
<tr *ngFor="let data of tabledata">
<td>{{data.name}}</td>
<td>{{data.age}}</td>
</tr>
Could you please try to excute your Get method in OnInit?您能否尝试在 OnInit 中执行您的 Get 方法? Why do your Get method starts with a capital letter?
为什么您的 Get 方法以大写字母开头? Why do you declare your table as a table of any?
为什么将您的表声明为任何表? Using typed objects i always better.
使用类型化的对象我总是更好。 Can you try this code below?
你可以试试下面的代码吗?
vartabledata: Person[];
class MyComponent implements OnInit {
ngOnInit() { this.get()
}
get(){
this.getservice().subscribe(
res=> {this.tabledata = res.data;
console.log(this.tabledata);}
)}
}
In front i would suggest you to add *ngIf so you display table only when your data are loaded.在前面,我建议您添加 *ngIf 以便仅在加载数据时显示表格。
<div *ngIf="tabledata">
<tr *ngFor="let data of tabledata">
<td>{{data.name}}</td>
<td>{{data.age}}</td>
</tr>
</div>
If there are no data even with this, try to do如果即使这样也没有数据,请尝试执行
<div *ngIf="tabledata">
<tr *ngFor="let data of tabledata">
{{data | json}}
</tr>
</div>
I think that you receive a data object that doesn't contains name and age attributes我认为您收到的数据 object 不包含姓名和年龄属性
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.