繁体   English   中英

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

[英]Dynamically add an object property to existing object

所以我有一个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属性与chart属性一起添加,如上所示。 我怎么做?

像这样...

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

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM