Angular 如何仅在数据值为>;角度为0
我有一些来自web api的数据,我只想在其值大于0时显示这些数据。到目前为止,我所做的并没有实现这一点,即: 数据结构示例:Angular 如何仅在数据值为>;角度为0,angular,Angular,我有一些来自web api的数据,我只想在其值大于0时显示这些数据。到目前为止,我所做的并没有实现这一点,即: 数据结构示例: result: Array(1) 0: element: "xxxxx" element_id: "xxxx" name: "xxxxx" etc etc etc 组件。ts receivedIncidentComments: any; constructor(private service: nowService, private appComponent
result: Array(1)
0:
element: "xxxxx"
element_id: "xxxx"
name: "xxxxx"
etc etc etc
组件。ts
receivedIncidentComments: any;
constructor(private service: nowService,
private appComponent: AppComponent,
private userService: UserService,
private router: Router,
private http: HttpClient,
private route: ActivatedRoute
) {
this.receivedIncidentComments = { created_on: '', value: ''}
}
private getIncidentComments() {
this.service.getIncidentComments(this.customer_id, this.sys_id).subscribe((data) => {
console.log('Result - ', data);
console.log('comments are received');
this.receivedIncidentComments = data.result[0];
})
}
ngOnInit() {
this.getIncident();
this.getIncidentComments();
})
}
getIncidentComments(customerId, sys_id): Observable<any> {
return this.http.get<any>(this.commentsApiUrl + "?customer_id=" + customerId + "&incident_id=" + sys_id)
.pipe(
catchError(this.handleError)
);
}
}
服务.ts
receivedIncidentComments: any;
constructor(private service: nowService,
private appComponent: AppComponent,
private userService: UserService,
private router: Router,
private http: HttpClient,
private route: ActivatedRoute
) {
this.receivedIncidentComments = { created_on: '', value: ''}
}
private getIncidentComments() {
this.service.getIncidentComments(this.customer_id, this.sys_id).subscribe((data) => {
console.log('Result - ', data);
console.log('comments are received');
this.receivedIncidentComments = data.result[0];
})
}
ngOnInit() {
this.getIncident();
this.getIncidentComments();
})
}
getIncidentComments(customerId, sys_id): Observable<any> {
return this.http.get<any>(this.commentsApiUrl + "?customer_id=" + customerId + "&incident_id=" + sys_id)
.pipe(
catchError(this.handleError)
);
}
getIncidentComments(customerId,sys\u id):可观察{
返回this.http.get(this.commentsApiUrl+“?customer_id=“+customerId+”)和incident_id=“+sys_id”)
.烟斗(
catchError(this.handleError)
);
}
html:
<ng-container *ngIf="receivedIncidentComments.length > 0">
<div class="information-text">
<h5>Comments:</h5>
<span class="text-gray">{{receivedIncidentComments.created_on}}</span><br />
<span class="text-gray">{{receivedIncidentComments.value}}</span>
</div>
</ng-container>
评论:
{{receivedIncidentComments.created_on}}
{{receivedIncidentComments.value}}
有什么想法吗?替换
与
试试这个:
<ng-container *ngIf="receivedIncidentComments.length > 0">
<div class="information-text">
<h5>Comments:</h5>
<span class="text-gray">{{receivedIncidentComments.created_on}}</span><br />
<span class="text-gray">{{receivedIncidentComments.value}}</span>
</div>
</ng-container>
评论:
{{receivedIncidentComments.created_on}}
{{receivedIncidentComments.value}}
您需要提供更多信息。您是否将传入数据存储在实例变量中?收到意外评论?那么
*ngIf="receivedIncidentComments.length > 0"
您的*ngIf指令语法正确。With可能意味着您没有使用正确的成员构造布尔/逻辑语句。请共享整个TS和HTML文件:)从您的HTML结构可以清楚地看出receivedIncidentComments是一个对象。 因此,要找到对象的长度,可以使用以下公式
Object.key(receivedIncidentComments).length代码>
希望您现在可以实现自己的逻辑。
只需将对象长度存储在component.ts文件中即可。
现在在html中检查它
您可以按如下方式使用它
*ngIf=“your_variable>0”
什么是*ngIf=“data.length>0”
检查?什么是数据
?您是否应该使用receivedIncidentComments
?请使用data
的值更新问题。您没有为data
属性分配任何内容。data
是组件的公共属性吗?更新了代码。为什么要在ngIf
条件下测试data.length
?请尝试使用
。您是否可以在服务中发布您的函数getIncidentComments
。我们需要知道服务返回的数据是什么。您是否发现
有任何错误?我认为您应该得到一个错误,因为receivedIncidentComments
是一个对象。请尝试*ngIf=“receivedIncidentComments”
。如果这不起作用,请向我们展示您收到的comments对象的示例数据。对于e.x,console.log(data.result[0])的结果
我已经用收到的数据的结构更新了代码。没有错误*ngIf=“receivedIncidentComments”有效。你能补充一下答案吗?我会接受的。