A.Mujeeb
A.Mujeeb

Reputation: 51

Use value from model, or if it is undefined then use default value

Below is my html code

<div>IsValid : {{component._isValid || true}}</div>

If component._isValid is undefined, it should show true but if it is defined, it should show its value. Currently it is showing true only.

Upvotes: 0

Views: 69

Answers (3)

Umut
Umut

Reputation: 37

Use this code instead:

<div>IsValid : {{component._isValid ? component._isValid : true}}</div>

Upvotes: 0

Neeraj Kamat
Neeraj Kamat

Reputation: 354

The below line should do the needful. AngularJS directly identifies ternary operation in HTML.

<div>IsValid : {{typeof(component._isValid) === "undefined" ? true: component._isValid }}</div>

Upvotes: 0

Bill P
Bill P

Reputation: 3618

This is not a so simple condition, so you should better use a function in your controller:

isValid() {
    return angular.isUndefined(component._isValid) ? true : component._isValid;
}

Call it in template with one-time binding if value is not changing:

<div>IsValid : {{ isValid() }}</div>

<div>IsValid : {{ ::isValid() }}</div>

Upvotes: 1

Related Questions