在 angularjs 中从 Rest 返回到 "null" 的检查列表

Checking List returned from Rest to "null" in angularjs

我无法检查从 REST 返回的列表的状态,无论返回的列表是否为空,数据以以下格式返回 ["IOC","Mycompany","Test"]

 Test.save($scope.Data,
                   function(data){
                       //if(data==null) not working  
                       if(data[0]==null)//not working too
                           {
                           alert("empty")
                           }
                         });

如何判断返回列表是否为空???

您可以将检查空数组的逻辑更改为此 -

if (data && data.length == 0) {
    alert("empty")
}

首先,验证当列表为空时您实际得到的是什么。

您应该检查空列表的长度,而不是尝试对其进行索引:

if ((data || []).length === 0) {
    // empty
}

Angular 提供函数 angular#isObject

Determines if a reference is an Object. Unlike typeof in JavaScript, nulls are not considered to be objects. Note that JavaScript arrays are objects.

Test.save($scope.Data, function(data){
    if(!angular.isObject(data)){ 
       alert("empty")
    }
});