undefined
当我编写手表处理函数时,我会在和上检查 newVal 参数null
。为什么 AngularJS 有这样的行为,却没有特定的实用方法?所以有angular.isUndefined
但没有angular.isUndefinedOrNull
。手动实现它并不难,但是如何扩展角度以在每个控制器中具有该功能?肿瘤坏死因子。
编辑:
这个例子:
$scope.$watch("model", function(newVal) {
if (angular.isUndefined(newVal) || newVal == null) return;
// do somethings with newVal
}
处理这种方式是普遍接受的做法吗?
编辑 2:
JSFiddle 示例(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);
});
};