【问题标题】:How to set a condition before assigning value to a property in html and angular?如何在为html和angular中的属性赋值之前设置条件?
【发布时间】:2017-10-14 05:37:55
【问题描述】:
<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 中。我怎样才能做到这一点?可以使用三元运算符吗?

【问题讨论】:

    标签: html angular angular2-template angular2-forms


    【解决方案1】:

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

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

    或者调用一个返回适当值的函数:

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

    希望这会有所帮助。

    【讨论】:

    • 那行得通。我缺少的是将标题括在括号中。非常感谢!
    • 非常有用,例如,当您只想将值设置为不为空的属性时:[max]="endDate !== null ? endDate: nowDate"(来自 Angular 的 datepicker :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-04
    • 2016-07-04
    • 1970-01-01
    • 1970-01-01
    • 2020-04-25
    • 2015-03-01
    相关资源
    最近更新 更多