80

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);
    });
};
4

6 回答 6

160

您可以随时为您的应用程序添加它

angular.isUndefinedOrNull = function(val) {
    return angular.isUndefined(val) || val === null 
}
于 2013-07-28T19:15:39.207 回答
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;
});

或者您也可以将其添加到 rootScope。使用您自己的实用程序函数扩展 angular 的几个选项。

于 2013-07-28T17:07:41.423 回答
14

不久前我问了 lodash 维护者同样的问题,他们回答说!=可以在这里使用操作符:

if(newVal != null) {
  // newVal is defined
}

这使用 JavaScript 的类型强制来检查undefinedor的值null

如果您使用 JSHint 对代码进行 lint,请添加以下注释块来告诉它您知道自己在做什么 - 大多数情况下!=被认为是错误的。

/* jshint -W116 */ 
if(newVal != null) {
/* jshint +W116 */
  // newVal is defined
}
于 2014-07-25T11:58:00.283 回答
9

为什么不简单地使用angular.isObject否定呢?例如

if (!angular.isObject(obj)) {
    return;
}
于 2014-06-17T15:27:37.973 回答
7

@STEVER 的回答令人满意。但是,我认为发布一种稍微不同的方法可能会很有用。我使用一个名为 isValue 的方法,它对除 null、undefined、NaN 和 Infinity 之外的所有值返回 true。将 NaN 与 null 和 undefined 合并是该函数对我的真正好处。将 Infinity 与 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)));
};

或作为工厂的一部分

.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)));
  };
})
于 2014-11-24T20:49:47.180 回答
3

lodash提供了一种速记方法来检查 undefined 或 null: _.isNil(yourVariable)

于 2016-08-23T19:56:39.890 回答