如何在给html和angular中的属性赋值前设置条件?

How to set a condition before assigning value to a property in html and angular?

<div #dataContainer class="infills richtextbox {{field.groupid}}"
                    contentEditable="true" 
                    *ngIf="field.fieldtype=='text'"
                    id="{{field.id}}" 
                    name="{{field.id}}" 
                    [ngStyle]="{'position':'absolute','top':field.y+'px','left':field.x+'px','font-size': field.fontsize,'font-family':field.fontfamily,'width':field.width+'px','height':field.height+'px','border':'1px solid #FFF','background':'#EEE','overflow':'hidden' }"
                    title="{{field.description}}"
                    [(ngModel)]="field.value" 
                    (keypress)="_keyPress($event,field.pattern)"
                    (dblclick)="openFullRTE(field.id)" 
                    (focusout)="field.htmlContent=dataContainer.innerHTML;">
</div>

在这里,我想看看 field.description 是否有值 "desc",如果有,将空值分配给 title 属性,否则它可以包含 field.description 中的值。我怎样才能做到这一点?可以使用三元运算符吗?

您可以使用三元运算符,甚至可以调用一个函数。首先,您需要将 title 属性括在方括号中。它看起来像这样:

[title]="field.description == 'desc' ? 'some value' : ''"

或调出 returns 适当值的函数:

[title]="getFieldDescription(field.description)"

希望对您有所帮助。