Make sure the value is not empty and not empty in the angular template?

How to check that the value is not empty, not empty.

in the controller: $scope.data.variable = 'some valid data';

<div ng-if="data.variable != ''">something else</div>

thank

+4
source share
3 answers

since both null and empty are false values

div.variable if not nullor emptywill evaluate as true, and if any of nullor emptyevaluates to false

 <div ng-if="data.variable">something else</div>
+7
source

I suggest that this function be your controller:

$scope.isEmpty = function(value){
return (value == "" || value == null);
};

And your HTML:

<div ng-if="!isEmpty(data.variable)">something else</div>

If this feature is useful on many of your pages, I suggest you put it on $ rootScope so that it is recognized throughout the application.

+5
source

Be that as it may, in your case, I think you are expecting either an empty string or null. To write a reasonable check, I find it best to do this;

$scope.isValid = function(val) {
    var ret = true;
    if (angular.isString(val)) {
        ret = ret && (val.length > 0);
    }
    return ret && (val !== null);
}
0
source

Source: https://habr.com/ru/post/1568865/


All Articles