Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Undefined or null for AngularJS

When I write watch handling functions I check newVal param on undefined and null. Why does AngularJS have such a behavior, but doesn't have particular utility method? So there is angular.isUndefined but not angular.isUndefinedOrNull. It isn't hard to implement that by hand but how extend angular to have that function in each controller? Tnx.

Edit:

The example:

$scope.$watch("model", function(newVal) {     if (angular.isUndefined(newVal) || newVal == null) return;     // do somethings with newVal } 

Is it common accepted practice to handle such a way?

Edit 2:

The JSFiddle example (http://jsfiddle.net/ubA9r/):

<div ng-app="App">   <div ng-controller="MainCtrl">        <select ng-model="model" ng-options="m for m in models">           <option value="" class="ng-binding">Choose model</option>       </select>       {{model}}   </div> </div>  var app = angular.module("App", []);  var MainCtrl = function($scope) {     $scope.models = ['Apple', 'Banana'];     $scope.$watch("model", function(newVal) {         console.log(newVal);     }); }; 
like image 664
slo2ols Avatar asked Jul 28 '13 16:07

slo2ols


People also ask

Is null or empty in angular?

How to check if a variable string is empty or undefine or null in Angular. In template HTML component: We can use the ngIf directive to check empty null or undefined. In this example, if stringValue is empty or null, or undefined, It prints the empty message.

How check variable is empty or not in Angularjs?

You can use angular's function called angular. isUndefined(value) returns boolean. Show activity on this post. if(!a) { // do something when `a` is not undefined, null, ''. }

How many times does null and undefined occur in angular?

A brief search shows that (in the Angular code base starting from ./packages) assignment to null ( = null, notice the leading space to avoid matching === null) occurs 1448 times, and assignment to undefined ( = undefined) can be found 1905 times.

How to return a Boolean from an undefined value in angular?

You can use angular's function called angular.isUndefined (value) returns boolean. You may read more about angular's functions here: AngularJS Functions (isUndefined) if (!a) { // do something when `a` is not undefined, null, ''. } (!value) ?

How to check if a value is null or undefined?

Typescript component, Written a function for checking null or undefined or empty using typeOf operator It is check for undefined value and returns true if it is null In template html component: We can use ngIf directive to check empty null or undefined.

What is the use of isUndefined in angular?

AngularJS | angular.isUndefined () Function Last Updated : 12 Apr, 2019 The angular.isUndefined () function in AngularJS is used to determine the value inside isUndefined function is undefined or not. It returns true if the reference is undefined otherwise returns false.


1 Answers

You can always add it exactly for your application

angular.isUndefinedOrNull = function(val) {     return angular.isUndefined(val) || val === null  } 
like image 177
Stepan Suvorov Avatar answered Sep 29 '22 04:09

Stepan Suvorov