0

I know this has been answered previously, but none of those solutions are working for me. Can some one please help me with the error below?

I am getting an error using Ng-Repeat with Custom filters when I am not getting an Array.

Can anyone help me to comeup with controller to use ng-repeat which will handle for both JSONObject and JSONArray

HTML Code

<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>

Error Will Produce when I have ServiceDisconnects section as below

 "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"
  }
}

But the same thing will work when the ServiceDisconnects section has multiple Service Arrays.

Here is the Custom Filter

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 Answer 1

1

I'm assuming you're only showing a subset of the json in your post?

Your Service property in your json is an object, not an array. To iterate over object properties, you'll need to use the other form of ng-repeat which is:

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

You're also going to want to update your filterWithOr filter to reflect those changes since you're not dealing with arrays.

Couple of things to note according to the ng-repeat Angular docs with using (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.

Sign up to request clarification or add additional context in comments.

1 Comment

F I agree with answer but is there any solution where i can controller which will help me to use ng-repeat for both JSONObject and JSONArray since my JSON data some times comes as array and sometime as Object

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.