2016-10-22 89 views
0

我知道这已被回答以前,但没有任何解决方案为我工作。有人可以帮我解决下面的错误吗?错误:[过滤器:notarray]预期的阵列,但收到

我正在使用伍重复自定义过滤的时候,我没有得到一个数组的错误。

谁能帮我带控制器使用NG重复将处理两者的JSONObject和JSONArray

到川方

HTML代码时,我有ServiceDisconnects部分如下

<td class="features" ng-show="list.Body.WorkOrder.OrderStatus.length > 0"> 
<span ng-repeat="indi in list.Body.Services.ServiceChanges.ServiceDisconnects.Service | filterWithOr:{_serviceID:['SS001','ST099','SS018']} ">{{indi._serviceID}},{{indi.EPCServiceDef.EPCProduct._pn}}<br></span> 
</td> 

错误将生产

"ServiceDisconnects": { 
    "Service": { 
    "-serviceID": "ST099", 
    "ServiceChangeActivity": "Disconnect", 
    "TelephoneNumbers": { 
     "TN": "     " 
    }, 
    "Rate": "0.00", 
    "Desc": "Xhnoecosvr", 
    "Count": "0", 
    "LOB": "Other", 
    "PackageCode": "ST099 ", 
    "EPCServiceDef": { 
     "EPCProduct": { 
     "-pn": "10400138", 
     "Name": "EcoSaver Opt-Out Tracking", 
     "LongDescription": "NO ECOSAVER TRACKING", 
     "Type": "Feature", 
     "LOB": "Video" 
     } 
    }, 
    "CSGServiceIdentifier": "5" 
    } 
} 

但是,同样的事情将工作时,ServiceDisconnects部分mult iple服务阵列。

这里是自定义筛选

app.filter('filterWithOr', function($filter) { 
    var comparator = function(actual, expected) { 
     if (angular.isUndefined(actual)) { 
      // No substring matching against `undefined` 
      return false; 
     } 
     if ((actual === null) || (expected === null)) { 
      // No substring matching against `null`; only match against `null` 
      return actual === expected; 
     } 
     if ((angular.isObject(expected) && !angular.isArray(expected)) || (angular.isObject(actual) && !hasCustomToString(actual))) { 
      // Should not compare primitives against objects, unless they have custom `toString` method 
      return false; 
     } 

     actual = angular.lowercase('' + actual); 
     if (angular.isArray(expected)) { 
      var match = false; 
      expected.forEach(function(e) { 
       e = angular.lowercase('' + e); 
       if (actual.indexOf(e) !== -1) { 
        match = true; 
       } 
      }); 
      return match; 
     } else { 
      expected = angular.lowercase('' + expected); 
      return actual.indexOf(expected) !== -1; 
     } 
    }; 
    return function(campaigns, filters) { 
     return $filter('filter')(campaigns, filters, comparator); 
    }; 
}); 

回答

1

我假设你只显示在您的文章中json的一个子集?

您的0123B您的json属性是一个对象,而不是一个数组。遍历对象的属性,你需要使用的ng-repeat另一种形式是:

<div ng-repeat="(key, value) in myObj"></div> 

你也将需要更新您的filterWithOr过滤器,以反映这些变化,因为你不处理阵列。

几件事情要根据ng-repeat Angular docs注意使用(key, value)

The JavaScript specification does not define the order of keys returned for an object, so Angular relies on the order returned by the browser when running for key in myObj. Browsers generally follow the strategy of providing keys in the order in which they were defined, although there are exceptions when keys are deleted and reinstated. See the MDN page on delete for more info.

ngRepeat will silently ignore object keys starting with $, because it's a prefix used by Angular for public ($) and private ($$) properties.

The built-in filters orderBy and filter do not work with objects, and will throw an error if used with one.

+0

FI与答案达成一致,但没有任何解决方案,我能控制器,它会帮助我使用NG-重复两者的JSONObject和JSONArray自我的JSON数据有时会以数组的形式出现,有时会以Object的形式出现 – Batman