check null,empty or undefined angularjs
just use -
if(!a) // if a is negative,undefined,null,empty value then...
{
// do whatever
}
else {
// do whatever
}
this works because of the == difference from === in javascript, which converts some values to "equal" values in other types to check for equality, as opposed for === which simply checks if the values equal. so basically the == operator know to convert the "", null, undefined to a false value. which is exactly what you need.
If value is 0 then return empty string in Angularjs
You can do it like this:
{{profileCtrl.person.year === 0 ? "" : profileCtrl.person.year}}
How do I check for an empty/undefined/null string in JavaScript?
Empty string, undefined, null, ...
To check for a truthy value:
if (strValue) {
// strValue was non-empty string, true, 42, Infinity, [], ...
}
To check for a falsy value:
if (!strValue) {
// strValue was empty string, false, 0, null, undefined, ...
}
Empty string (only!)
To check for exactly an empty string, compare for strict equality against ""
using the ===
operator:
if (strValue === "") {
// strValue was empty string
}
To check for not an empty string strictly, use the !==
operator:
if (strValue !== "") {
// strValue was not an empty string
}
AngularJS - What is the best way to detect undefined null or empty value at the same time?
Update
As mentioned in the comments it's better to use return !value
.
$scope.isValid = function(value) {
return !value
}
Old and incomplete Answer
A proper way is simply to use angular.isDefined()
How to display empty string when ng-repeat value is null in angularjs
Zero is falsy. Use ternary operator:
{{ x.daysSinceMaintenanceUpdate == null ? '--' : x.daysSinceMaintenanceUpdate }}
Related Topics
How to Get Route Url Params in a Page in Nuxt2 and 3
Node.Js Document Is Not Defined
Datatables/Tabletools: Format Data as Text When Exporting to Excel
Use JavaScript to Place Cursor At End of Text in Text Input Element
React.Js - Input Losing Focus When Rerendering
How to Convert Postgresql Time Without Time Zone to Date in JavaScript
How to Check If a Background Image Is Loaded
How to Display All Imagefrom Array in Reactjs
Check If Two Div HTML Elements Intersect
Typeerror: Cannot Create Property '_Id' on String
How to Change Background Color of a Row on Checkbox Selection
What Does an Exclamation Mark Before a Variable Mean in JavaScript
How to Do Validation for 10 Digit Mobile Number Using Jquery
Prevent User from Typing in Input At Max Value
Getting Selected Values from Dropdown Inside Table
How to Add Counter in Angular 6
How to Make Any Hovered Color Darker/Lighter That the Original