Sireini
Sireini

Reputation: 4262

AngularJS ngIf object has attribute

I have an array like this:

{
    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"]
}

Now I want to do this in my 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>

Is it possible to check if the object contains an attribute like valueChoices than show the HTML like above. Or is this not the right way to do it?

Upvotes: 3

Views: 2101

Answers (1)

Pankaj Parkar
Pankaj Parkar

Reputation: 136174

Yes, you could use Elvis operator

*ngIf="prototype?.valueChoices"

Where ? will act like a ternary operator. Here prototype?'s ? will ensure that next expression .valueChoices would get read until prototype has value. The advantage of using Elvis operator is you can n number of ? to handle variable existence check like a?.b?.?c.?d

Upvotes: 6

Related Questions