如何在angularjs的视图部分中将字符串转换为JSON数据

问题描述 投票:0回答:1

嗨,我有JSON格式的数据,我想将选项的这些数据转换为json,以便我可以在angularjs的视图部分调用optionName,optionSubName

    [
    {
      "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
    }
  ]

我已经尝试过这个但是这不能使用ng-repeat我使用这个{{data.option.optionName | JSON}}但是这不适用于我的视角部分angularjs,请帮助我如何达到我的目标?

angularjs angularjs-ng-repeat
1个回答
0
投票

使用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);  
               })
            });
        });

      }
    ]); 
© www.soinside.com 2019 - 2024. All rights reserved.