我正在尝试使用字符串插值将数据绑定到模板。当我试图引用模板中的变量时,它返回以下错误:
js:1350错误类型错误:无法读取的属性“status”
未定义。
HTML代码:

<div class="chat-main">
    <div class="col-md-12 chat-header rounded-top bg-primary text-white hide-chat-box">
        <div class="row">
            <div class="col-md-6 username pl-2">
                <h6 class="m-0">&nbsp;&nbsp; &nbsp;Tasks</h6>&nbsp;
                <span class="badge" style="background: red">{{ task?.status?.length }}</span>
            </div>
            <div class="col-md-6 options text-right pr-2">
                <i class="fa fa-window-minimize" aria-hidden="true"></i>
            </div>
        </div>
    </div>
    <div class="chat-content">
        <div class="col-md-12 chats border">
            <br/>
            <ul class="p-0" *ngFor="let task of tasksRes">
                <li class="pl-2 pr-2 text-dark send-msg mb-1">
                    <div>
                        <a href="javascript:;" [routerLink]="[task.link]" style="text-decoration: none !important;">
                            <span class="text-warning" *ngIf="task.status == 'In_progress'"><i class="fa fa-spinner fa-spin"></i></span>
                            <span class="text-success" *ngIf="task.status == 'Done'"><i class="fa fa-check"></i></span>
                            <span>&nbsp;&nbsp;{{ task.name }}</span>
                           </a>
                        <br/>
                        <span class="pull-right text-muted">{{task.createdDate | timeAgo}}</span>&nbsp;&nbsp;
                        <span class="text-muted">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{{task.eventType}}</span>
                    </div>
                </li>
            </ul>
        </div>
    </div>
</div>

最佳答案

task是指ul元素的模板内的局部变量范围
只需将这个span元素移到“ngfor”模板中

<span class="badge" style="background: red">{{ task?.status?.length }}</span>

NgFor Local Variables

关于javascript - 字符串插值, Angular 不绑定(bind),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51943810/

10-11 14:19