Reputation: 510
<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
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
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
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