0

Can anyone help me with below query ?

Sometimes I will get the data as JSONObject and sometimes I will get the data as JSONArray during the time of ng-repeat it will only work with JSONArray but not with JSONObject situation Can you suggest me something here?

Here is the example this will show the value of _serviceID only when Service comes as an array but this won't work if Service section comes as an Object

<td class="features">
<span ng-repeat="indi in ServiceChanges.ServiceInstalls.Service">{{indi._serviceID}}<br></span>
</td>

Note : I don't want to use ng-if to check the length as it affects the position of my DOM in other DIV So is there any solution other than this kind of approach like converting object into array by adding dummy values?

1 Answer 1

1

You could convert the return response into an array in controller.

.controller(function ($scope) {
    // code here

    var serviceArray = [];
    var response = angular.copy($scope.ServicesChanges.ServiceInstalls.Service);

    if (angular.isObject(response)) {
        serviceArray.push(response);
        $scope.serviceArray = serviceArray;
    } else if (angular.isArray(response)) {
        $scope.serviceArray = response;
    } else {
        $scope.serviceArray = serviceArray;
    }

    // code here
});

<td class="features">
    <span ng-repeat="indi in serviceArray">{{indi._serviceID}}<br></span>
</td>
Sign up to request clarification or add additional context in comments.

6 Comments

@Batman which part is not working? what output do you get for $scope.serviceArray after you implemented above code?
The loop is going to else condition and when i check in Console it shows undefined
what do you get for ServicesChanges.ServiceInstalls.Service initially in your code?
inline { "PackageCode": "ST099", "CSGServiceIdentifier": "5", "_serviceID": "ST099" } I was getting like this
@Batman I guess something gone wrong in some part of your code. Most probably that if you use the above code var response = angular.copy($scope.ServicesChanges.ServiceInstalls.Service); does not give you expected outcome of your response. Take a look at this jsfiddle i created. jsfiddle.net/Lvc0u55v/11091 It works.
|

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.