指向
https://angular.io/docs/ts/latest/guide/displaying-data.html和堆栈
How to check empty object in angular 2 template using *ngIf仍然得到语法错误自身上下文未定义.如果我删除* ngIf条件,那么我在teamMembers中获取值,如果我推一些值,我可以访问teamMembers中的值.
我的teamMember对象是[]数组我试图检查条件数组是空的大小.
尝试:
<div class="row" *ngIf="(teamMembers | json) != '{}'">
和
<div class="row" *ngIf="teamMembers.length > 0"> //Check length great than throwing Syntax error <div class="col-md-12"> <h4>Team Members</h4> <ul class="avatar" *ngFor="let member of teamMembers"> <li><a href=""><gravatar-image [size]="80" [email]="member.email"></gravatar-image></a></li> </ul> </div> </div>
零件 :
@Component({ selector: 'pbi-editor',}) export class AppComponent implements OnInit { teamMembers: User[];
任何帮助都会很棒.
<div class="row" *ngIf="teamMembers?.length > 0">
首先检查teamMembers是否具有值,如果teamMembers没有值,则它不会尝试访问未定义的长度,因为条件的第一部分已经失败.