slo*_*ols 80 null undefined angularjs
当我编写手表处理函数时,我检查newVal参数undefined和null.为什么AngularJS有这样的行为,但没有特定的实用方法?所以有,angular.isUndefined但没有angular.isUndefinedOrNull.手动实现并不难,但是如何扩展角度以在每个控制器中具有该功能?TNX.
编辑:
这个例子:
$scope.$watch("model", function(newVal) {
if (angular.isUndefined(newVal) || newVal == null) return;
// do somethings with newVal
}
Run Code Online (Sandbox Code Playgroud)
处理这种方式是否是普遍接受的做法?
编辑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);
});
};
Run Code Online (Sandbox Code Playgroud)
Ste*_*rov 158
您始终可以为您的应用程序添加它
angular.isUndefinedOrNull = function(val) {
return angular.isUndefined(val) || val === null
}
Run Code Online (Sandbox Code Playgroud)
luc*_*uma 17
我的建议是编写自己的公用事业服务.您可以在每个控制器中包含该服务或创建父控制器,将实用程序服务分配给您的作用域,然后每个子控制器都将继承它,而不必包含它.
示例:http: //plnkr.co/edit/NI7V9cLkQmEtWO36CPXy?p = preview
var app = angular.module('plunker', []);
app.controller('MainCtrl', function($scope, Utils) {
$scope.utils = Utils;
});
app.controller('ChildCtrl', function($scope, Utils) {
$scope.undefined1 = Utils.isUndefinedOrNull(1); // standard DI
$scope.undefined2 = $scope.utils.isUndefinedOrNull(1); // MainCtrl is parent
});
app.factory('Utils', function() {
var service = {
isUndefinedOrNull: function(obj) {
return !angular.isDefined(obj) || obj===null;
}
}
return service;
});
Run Code Online (Sandbox Code Playgroud)
或者您也可以将其添加到rootScope.使用您自己的实用程序函数扩展角度的几个选项.
Tom*_*cer 14
我前一段时间问过lodash维护者的相同问题,他们回答提到!=操作符可以在这里使用:
if(newVal != null) {
// newVal is defined
}
Run Code Online (Sandbox Code Playgroud)
这使用JavaScript的类型强制来检查undefined或的值null.
如果您使用JSHint来填充代码,请添加以下注释块以告诉它您知道自己在做什么 - 大部分时间都!=被认为是错误的.
/* jshint -W116 */
if(newVal != null) {
/* jshint +W116 */
// newVal is defined
}
Run Code Online (Sandbox Code Playgroud)
为什么不简单地使用angular.isObject否定?例如
if (!angular.isObject(obj)) {
return;
}
Run Code Online (Sandbox Code Playgroud)
@STEVER的回答令人满意.但是,我认为发布略有不同的方法可能会有所帮助.我使用一个名为isValue的方法,它为除null,undefined,NaN和Infinity之外的所有值返回true.在NaN中使用null和undefined进行集中是我的功能的真正好处.使用null和undefined来集中无限是更有争议的,但坦率地说,对我的代码来说并不那么有趣,因为我几乎从不使用Infinity.
以下代码的灵感来自Y.Lang.isValue.这是Y.Lang.isValue 的来源.
/**
* A convenience method for detecting a legitimate non-null value.
* Returns false for null/undefined/NaN/Infinity, true for other values,
* including 0/false/''
* @method isValue
* @static
* @param o The item to test.
* @return {boolean} true if it is not null/undefined/NaN || false.
*/
angular.isValue = function(val) {
return !(val === null || !angular.isDefined(val) || (angular.isNumber(val) && !isFinite(val)));
};
Run Code Online (Sandbox Code Playgroud)
或作为工厂的一部分
.factory('lang', function () {
return {
/**
* A convenience method for detecting a legitimate non-null value.
* Returns false for null/undefined/NaN/Infinity, true for other values,
* including 0/false/''
* @method isValue
* @static
* @param o The item to test.
* @return {boolean} true if it is not null/undefined/NaN || false.
*/
isValue: function(val) {
return !(val === null || !angular.isDefined(val) || (angular.isNumber(val) && !isFinite(val)));
};
})
Run Code Online (Sandbox Code Playgroud)