3

Hi I have a code like following in my controller

myClientApp.controller('ListCtrl', function ($scope,$http,$cookieStore,$location, $routeParams) {
var data = {
          "menus": {
                "view": true,
                "add": true,
                "update": true,
                "delete": true
              },
              "linkInfo": {
                "labelColumn": "codeName",
                "linkColumn": "lookupKey",
                "urlInfo": "reference"
              },
              "resultList": [
                "{\"lookupKey\":2,\"clientKey\":1,\"codeName\":\"Application.AppType\",\"codeValue\":\"ApplicationType2\",\"codeDesc\":\"##\",\"updatedBy\":null,\"internalCodeName\":\"Application.AppType\"}",
                "{\"lookupKey\":3,\"clientKey\":1,\"codeName\":\"Application.Class\",\"codeValue\":\"Tier 1\",\"codeDesc\":\"Critical Application requiring immediate response in case of a disruption of Service\",\"updatedBy\":null,\"internalCodeName\":\"Application.Class\"}"
              ]
            };
    $scope.result = angular.fromJson(data.resultList);
    alert($scope.result[0].codeName);
});

And It gives me undefined . Why?

3
  • Have you tried $scope.result = angular.fromJson(data.resultList[0]); and then alert($scope.result.codeName);? Commented Apr 16, 2014 at 10:11
  • OH. So we have to loop through each object? Commented Apr 16, 2014 at 10:14
  • 1
    Yep mate use a simple for loop Commented Apr 16, 2014 at 10:23

1 Answer 1

13

Because resultList is an array of JSON strings, not a single JSON string; you need to specify which key you want to decode:

$scope.result = [
    angular.fromJson(data.resultList[0]),
    angular.fromJson(data.resultList[1])
];
alert($scope.result[0].codeName);
Sign up to request clarification or add additional context in comments.

1 Comment

I would recommend moving this into your own service too so it can be mocked out while testing. Unless there is an angular way to do it?

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.