将对象属性动态添加到现有对象

时间:2015-07-24 19:25:00

标签: javascript angularjs highcharts

所以我有一个json对象,例如:

"highChart":{
            "xAxis":{  
               "categories":[  
                  "SYN 13 ",
                  "Settlement",
                  "Service Interaction",
                  "FNOL",
                  "Repair Process",
                  "Rental Experience",
                  "Appraisal",
                  "SYN 14 "
               ],
               "title":{  
                  "text":""
               },
               "labels":{  
                  "enabled":true,
                  "maxStaggerLines":1
               },
               "tickWidth":1
            }}

让我们将其定义为$scope.chartConfig = highChart

现在我想向chartConfig添加另一个属性,如下所示:

"highChart":{
         "options":{
            "chart":{  
               "height":null,
               "type":"waterfall"
            }},
            "xAxis":{  
               "categories":[  
                  "SYN 13 ",
                  "Settlement",
                  "Service Interaction",
                  "FNOL",
                  "Repair Process",
                  "Rental Experience",
                  "Appraisal",
                  "SYN 14 "
               ],
               "title":{  
                  "text":""
               },
               "labels":{  
                  "enabled":true,
                  "maxStaggerLines":1
               },
               "tickWidth":1
            }}

因此,在初始化$scope.chartConfig = highChart之后,我想添加options属性以及图表属性,如上所示。我该怎么做?

2 个答案:

答案 0 :(得分:0)

就像这样...

$scope.chartConfig.options = {
  "chart":{  
    "height":null,
    "type":"waterfall"
 }
};

答案 1 :(得分:0)

不是100%清除您的要求,但可以使用angular.extend()合并对象

var options = {
    "chart": {
        "height": null,
        "type": "waterfall"
    }
}

angular.extend($scope.chartConfig, options);

$scope.chartConfig现在包含新属性。如果options的某些属性与原始属性相同但值不同,则原始值将使用这些值进行更新