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

时间:2019-03-08 07:33:46

标签: angularjs angularjs-ng-repeat

嗨,我有JSON格式的数据,我想将 option 的数据转换为json,以便我可以调用 optionName optionSubName 在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
    }
  ]

我已经尝试过此方法,但是使用ng-repeat无法解决此问题,但是我使用{{data.option.optionName | JSON}}却无法解决此问题,但是我认为angularjs部分无效,请帮助我目标?

1 个答案:

答案 0 :(得分:0)

为每个使用AngularJS:

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

      }
    ]);