AngularJS ngIf对象具有属性

时间:2016-03-24 11:19:10

标签: angular typescript

我有一个这样的数组:

{
    selector: "departureDate",
    selectorName: "Departure Date",
    constraints: ["GreaterThan", "LessThan", "Equals", "NotEquals"],
    valueType: "Date"
}, {
    selector: "arrivalDay",
    selectorName: "Arrival day(of week)",
    constraints: ["In"],
    valueType: "ChoiceList",
    valueChoices: ["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"]
}

现在我想在我的HTML中执行此操作:

<div class="col-xs-3" *ngIf="prototype.valueChoices">
            <select class="form-control" [(ngModel)]="expression.valueChoice">
                <option *ngFor="#valueChoice of prototype.valueChoices" [value]="constraint">
                    {{ valueChoice }}
                </option>
            </select>
        </div>

是否可以检查对象是否包含像valueChoices这样的属性,而不是像上面那样显示HTML。或者这不是正确的方法吗?

1 个答案:

答案 0 :(得分:6)

是的,您可以使用Elvis运算符

*ngIf="prototype?.valueChoices"

?将像三元运营商一样行事。此处prototype? ?将确保在.valueChoices具有值之前读取下一个表达式prototype。使用Elvis运算符的优点是n ?个数a?.b?.?c.?d可以处理变量存在检查,例如{{1}}