slo2ols slo2ols - 1 year ago 184
AngularJS Question

Undefined or null for AngularJS

When I write watch handling functions I check newVal param on

. Why does AngularJS have such a behavior, but doesn't have particular utility method? So there is
but not
. It isn't hard to implement that by hand but how extend angular to have that function in each controller? Tnx.


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 (

<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>

var app = angular.module("App", []);

var MainCtrl = function($scope) {
$scope.models = ['Apple', 'Banana'];
$scope.$watch("model", function(newVal) {

Answer Source

You can always add it exactly for your application

angular.isUndefinedOrNull = function(val) {
    return angular.isUndefined(val) || val === null 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download