Search code examples
angularjsangularjs-ng-repeat

How to convert string into JSON data in view part of angularjs


Hi I have this data in JSON form, and I want to convert this data of option into json so that i can call optionName, optionSubName in a view part of angularjs

    [
    {
      "heading": "MakeUp Type / Selection",
      "option": "{"optionName":"Bridal Makeup","optionSubName":"Engagement,Tika / Godh Bharai,Ring Ceremony","optionDesc":""}",
      "values": null
    },
    {
      "heading": "Makeup Type",
      "option": "{"optionName":"Experienced","optionSubName":"","optionDesc":{"products":"Bobbie Brown,Makeup Forever and Premium Makeup Products","Makeup_Include":"Straightening,Blow Drys,Tong Curls,Updo's","Drapping":"Yes","Lashes":"Yes","Nail_Polish_Change":"Yes","Extension_Available":"Yes","Airbrush_Available":"Yes"}}",
      "values": null
    }
  ]

I have already tried this but this is not working by using ng-repeat i have using this {{data.option.optionName | JSON}} but this is not working in my view part of angularjs, Please help me out how to reach at my goal ?


Solution

  • Use AngularJS forEach:

    angular.forEach(object, iterator, [context])
    
    object: The object interate over.
    iterator: The iterator function or the code.
    context: Object to become context (using this) for the iterator function.
    
    var app = angular.module('app', []);
        app.controller('AppController', ['$scope', '$http', 
          function($scope, $http) {
            $http.get('json/array.json').success(function(data) {
                $scope.array = data; 
                  angular.forEach($scope.array, function(x){
                       console.log(x.optionName);  
                   })
                });
            });
    
          }
        ]);