Harish
Harish

Reputation: 510

How to check type of variable inside ng-class?

<div ng-class="{
    'undefined' : typeof object.property === 'undefined',
    'edit-active' : open == true
  }"> 
      {{ object.property }} 
</div>

This gives me a syntax error:

Error: [$parse:syntax] Syntax Error: Token 'object.property' is unexpected, expecting [}]

How can I check if object.property is defined?

Note: I am using angular 1.2.28.

Upvotes: 1

Views: 2760

Answers (3)

Harish
Harish

Reputation: 510

This works too

Create a filter:

.filter('isUndefined', [function() {
  return function(input) {
    return typeof input === 'undefined';
  }
}]);

Use the filter in ng-class:

<div ng-class="{
    'empty' : object.property == '' || (object.property | isUndefined),
    'edit-active' : open == true
  }">
    {{object.property}}
</div>

Upvotes: 0

Muthukannan Kanniappan
Muthukannan Kanniappan

Reputation: 2079

typeof operator wouldnt work inside angular expression. However you can have a function in scope to do it.

<div ng-class="{
    'undefined' : checkType(object.property),
    'edit-active' : open == true
  }"> 
      {{ object.property }} 
</div>

In your controller:

$scope.checkType = function(prop) {
    return typeof prop === 'undefined';
}

Upvotes: 1

Drag13
Drag13

Reputation: 5988

If you need check is there smth, you can do much simplier

<div ng-class="{
    'undefined' : !object.property,
    'edit-active' :open == true
  }"> 
      {{ object.property }} 
</div>

Upvotes: 2

Related Questions