簡體   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