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);
});
};
You can always add it exactly for your application
angular.isUndefinedOrNull = function(val) {
return angular.isUndefined(val) || val === null
}