Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 578
Sireini Avatar asked Jul 07 '26 01:07

Sireini


1 Answers

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

like image 57
Pankaj Parkar Avatar answered Jul 09 '26 23:07

Pankaj Parkar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!