2013-07-28 38 views
69

當我編寫手錶處理函數時,我檢查undefinednull上的newVal參數。爲什麼AngularJS有這樣的行爲,但沒有特定的效用方法?所以有angular.isUndefined但不是angular.isUndefinedOrNull。手工實現並不困難,但是如何在每個控制器中擴展角度以實現該功能? TNX。未定義或爲AngularJS爲空

編輯

的例子:

$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); 
    }); 
}; 
+1

你可以切換到CoffeeScript的。有一個questionmark postifix運算符可以做到這一點。 –

+0

當你需要這樣的功能時,你能否給出真實的案例? –

+2

爲什麼不[失去未定義的檢查](http://stackoverflow.com/a/15992131/188246),只檢查'newVal == null'? –

回答

149

你總是可以準確地將其添加爲您的應用

angular.isUndefinedOrNull = function(val) { 
    return angular.isUndefined(val) || val === null 
} 
+0

我知道這是不是一件好事,延長JS庫這樣的方式,但它看起來很接近我的搜索。其實,我更感興趣的是爲什麼我堅持下去。在手錶處理程序中處理未定義和空值是否是標準做法? – slo2ols

+0

那麼,恕我直言,在良好的結構化應用程序中不應該有這種情況。 –

+0

明白了,thx。但我仍然認爲'null'是正確選擇的結果,它應該與'undefined'區分開來。 –

16

我給你的建議是寫自己的事業服務。您可以將服務包含在每個控制器中或創建一個父控制器,將實用程序服務分配給您的範圍,然後每個子控制器都會繼承此功能,而無需包含它。

例子: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爲好。只需使用您自己的實用功能來擴展角度的幾個選項。

+4

我堅信,效用函數不應該DI – slo2ols

+0

@ slo2ols封裝它可以創建一個服務,它僅注入到袋鼠 – lucuma

10

爲什麼不能簡單地用angular.isObject與否定?例如

if (!angular.isObject(obj)) { 
    return; 
} 
+0

它對基元類型不起作用。 –

+0

...但是如果你知道這個值是什麼類型並且可以選擇一個'angular.isX'方法來匹配,那麼這個選項是一個很好的選擇。 – Phasmal

+0

'null'是一個對象 – kedomonzter

11

I asked the same question of the lodash maintainers a while back,他們說通過提!=操作員可以用在這裏:

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

它使用JavaScript的類型強制檢查值undefinednull

如果您使用JSHint來爲您的代碼添加皮毛,請添加以下注釋塊以告訴它您知道自己在做什麼 - 大多數時間!=被認爲是不好的。

/* jshint -W116 */ 
if(newVal != null) { 
/* jshint +W116 */ 
    // newVal is defined 
} 
5

@ STEVER的答案是令人滿意的。不過,我認爲發佈稍微不同的方法可能會有所幫助。我使用了一個稱爲isValue的方法,該方法對除null,undefined,NaN和Infinity以外的所有值都返回true。在NaN中使用null和undefined是真正的功能對我來說的好處。用無效和未定義結合無窮大更具爭議性,但坦率地說,對於我的代碼來說沒有那麼有趣,因爲我幾乎從不使用Infinity。

以下代碼受Y.Lang.isValue的啓發。這裏是Y.Lang.isValue的source

/** 
* 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))); 
    }; 
}) 
+0

爲什麼不替換'val === null || !angular.isDefined(val)'只是'val == null'? –

2

lodash提供的速記方法的一部分,以檢查是否未定義或爲空: _.isNil(yourVariable)