angularjs $在控制器中的json加载范围

时间:2016-09-18 14:18:57

标签: javascript angularjs arrays json controller

我期待我的json商店包含" $ scope"不是外部文件中的initilaliser。 Enssuite将其加载到控制器中。 json文件的一个例子。

$scope.templates = {"webapp": {
              "DC01": [   
                        {
                          "name": "Groupe Froid 1A",
                          "value": "0",
                          "selected": "selected",
                          "data": {
                                    "status"            : $scope.DC01_GF1_STATUS,
                                    "capacite"          : $scope.DC01_GF1_CAP_T,
                                    "alarm"             : $scope.DC01_GF1_ALM,
                                    "tmpEntreeEG"       : $scope.DC01_GF1_TMP_EE,    
                                    "tmpSortieEG"       : $scope.DC01_GF1_TMP_SE,
                                    "alarm1"            : $scope.DC01_GF1_CURR_ALARM1, 
                                    "alarm2"            : $scope.DC01_GF1_CURR_ALARM2,
                                    "alarm3"            : $scope.DC01_GF1_CURR_ALARM3,
                                    "alarm4"            : $scope.DC01_GF1_CURR_ALARM4,
                                    "alarm5"            : $scope.DC01_GF1_CURR_ALARM5,
                                    "etatPompe1"        : $scope.DC01_GF1_CPUMP1,
                                    "etatPompe2"        : $scope.DC01_GF1_CPUMP2,
                                    "alarmPompe1"       : $scope.DC01_P_GF1_D_POMP1A1,
                                    "alarmPompe2"       : $scope.DC01_P_GF1_D_POMP2A1,
                                    "debit"             : $scope.DC01_P_GF1_DEB
                                  },
                        },
                        {
                          "name": "Groupe Froid 3A",
                          "value": "1",
                          "data": {
                                    "status"            : $scope.DC01_GF3_STATUS,
                                    "capacite"          : $scope.DC01_GF3_CAP_T,
                                    "alarm"             : $scope.DC01_GF3_ALM,
                                    "tmpEntreeEG"       : $scope.DC01_GF3_TMP_EE,    
                                    "tmpSortieEG"       : $scope.DC01_GF3_TMP_SE,
                                    "alarm1"            : $scope.DC01_GF3_CURR_ALARM1, 
                                    "alarm2"            : $scope.DC01_GF3_CURR_ALARM2,
                                    "alarm3"            : $scope.DC01_GF3_CURR_ALARM3,
                                    "alarm4"            : $scope.DC01_GF3_CURR_ALARM4,
                                    "alarm5"            : $scope.DC01_GF3_CURR_ALARM5,
                                    "etatPompe1"        : $scope.DC01_GF3_CPUMP1,
                                    "etatPompe2"        : $scope.DC01_GF3_CPUMP2,
                                    "alarmPompe1"       : $scope.DC01_P_GF3_D_POMP1A2,
                                    "alarmPompe2"       : $scope.DC01_P_GF3_D_POMP2A2,
                                    "debit"             : $scope.DC01_P_GF3_DEB
                                  },
                        },
                        {
                          "name": "Groupe Froid 4B",
                          "value": "2",
                          "data": {
                                    "status"            : $scope.DC01_GF4_STATUS,
                                    "capacite"          : $scope.DC01_GF4_CAP_T,
                                    "alarm"             : $scope.DC01_GF4_ALM,
                                    "tmpEntreeEG"       : $scope.DC01_GF4_TMP_EE,    
                                    "tmpSortieEG"       : $scope.DC01_GF4_TMP_SE,
                                    "alarm1"            : $scope.DC01_GF4_CURR_ALARM1, 
                                    "alarm2"            : $scope.DC01_GF4_CURR_ALARM2,
                                    "alarm3"            : $scope.DC01_GF4_CURR_ALARM3,
                                    "alarm4"            : $scope.DC01_GF4_CURR_ALARM4,
                                    "alarm5"            : $scope.DC01_GF4_CURR_ALARM5,
                                    "etatPompe1"        : $scope.DC01_GF4_CPUMP1,
                                    "etatPompe2"        : $scope.DC01_GF4_CPUMP2,                                        
                                    "alarmPompe1"       : $scope.DC01_P_GF4_D_POMP1B1,
                                    "alarmPompe2"       : $scope.DC01_P_GF4_D_POMP2B1,
                                    "debit"             : $scope.DC01_P_GF4_DEB                                      
                                  },
                        },
                        {
                          "name": "Groupe Froid 6B",
                          "value": "3",
                          "data": {
                                    "status"            : $scope.DC01_GF6_STATUS,
                                    "capacite"          : $scope.DC01_GF6_CAP_T,
                                    "alarm"             : $scope.DC01_GF6_ALM,
                                    "tmpEntreeEG"       : $scope.DC01_GF6_TMP_EE,    
                                    "tmpSortieEG"       : $scope.DC01_GF6_TMP_SE,
                                    "alarm1"            : $scope.DC01_GF6_CURR_ALARM1, 
                                    "alarm2"            : $scope.DC01_GF6_CURR_ALARM2,
                                    "alarm3"            : $scope.DC01_GF6_CURR_ALARM3,
                                    "alarm4"            : $scope.DC01_GF6_CURR_ALARM4,
                                    "alarm5"            : $scope.DC01_GF6_CURR_ALARM5,
                                    "etatPompe1"        : $scope.DC01_GF6_CPUMP1,
                                    "etatPompe2"        : $scope.DC01_GF6_CPUMP2,                                        
                                    "alarmPompe1"       : $scope.DC01_P_GF6_D_POMP1B2,
                                    "alarmPompe2"       : $scope.DC01_P_GF6_D_POMP2B2,
                                    "debit"             : $scope.DC01_P_GF6_DEB                                        
                                  },
                        }
                      ],
              "DC02": [
                        {
                          "name": "Groupe Froid 1A",
                          "value": "0",
                          "data": {
                                    "status"            : $scope.DC02_GF1_STATUS,
                                    "capacite"          : $scope.DC02_GF1_CAP_T,
                                    "alarm"             : $scope.DC02_GF1_ALM,
                                    "tmpEntreeEG"       : $scope.DC02_GF1_TMP_EE,    
                                    "tmpSortieEG"       : $scope.DC02_GF1_TMP_SE,
                                    "alarm1"            : $scope.DC02_GF1_CURR_ALARM1, 
                                    "alarm2"            : $scope.DC02_GF1_CURR_ALARM2,
                                    "alarm3"            : $scope.DC02_GF1_CURR_ALARM3,
                                    "alarm4"            : $scope.DC02_GF1_CURR_ALARM4,
                                    "alarm5"            : $scope.DC02_GF1_CURR_ALARM5,
                                    "etatPompe1"        : $scope.DC02_CVC_PMA_GF1_RM,
                                    "etatPompe2"        : $scope.DC02_CVC_PMB_GF1_RM,                                          
                                    "alarmPompe1"       : $scope.DC02_CVC_PMA_GF1_DF,
                                    "alarmPompe2"       : $scope.DC02_CVC_PMB_GF1_DF,
                                    "debit"             : $scope.DC02_DEBIT_GF1
                                  },
                        },
                        {
                          "name": "Groupe Froid 2A",
                          "value": "1",
                          "data": {
                                    "status"            : $scope.DC02_GF2_STATUS,
                                    "capacite"          : $scope.DC02_GF2_CAP_T,
                                    "alarm"             : $scope.DC02_GF2_ALM,
                                    "tmpEntreeEG"       : $scope.DC02_GF2_TMP_EE,    
                                    "tmpSortieEG"       : $scope.DC02_GF2_TMP_SE,
                                    "alarm1"            : $scope.DC02_GF2_CURR_ALARM1, 
                                    "alarm2"            : $scope.DC02_GF2_CURR_ALARM2,
                                    "alarm3"            : $scope.DC02_GF2_CURR_ALARM3,
                                    "alarm4"            : $scope.DC02_GF2_CURR_ALARM4,
                                    "alarm5"            : $scope.DC02_GF2_CURR_ALARM5,
                                    "etatPompe1"        : $scope.DC02_CVC_PMA_GF2_RM,
                                    "etatPompe2"        : $scope.DC02_CVC_PMB_GF2_RM,  
                                    "alarmPompe1"       : $scope.DC02_CVC_PMA_GF2_DF,
                                    "alarmPompe2"       : $scope.DC02_CVC_PMB_GF2_DF,
                                    "debit"             : $scope.DC02_DEBIT_GF2                                     
                                 },
                        },
                        {
                          "name": "Groupe Froid 4B",
                          "value": "2",
                          "data": {
                                    "status"            : $scope.DC02_GF4_STATUS,
                                    "capacite"          : $scope.DC02_GF4_CAP_T,
                                    "alarm"             : $scope.DC02_GF4_ALM,
                                    "tmpEntreeEG"       : $scope.DC02_GF4_TMP_EE,    
                                    "tmpSortieEG"       : $scope.DC02_GF4_TMP_SE,
                                    "alarm1"            : $scope.DC02_GF4_CURR_ALARM1, 
                                    "alarm2"            : $scope.DC02_GF4_CURR_ALARM2,
                                    "alarm3"            : $scope.DC02_GF4_CURR_ALARM3,
                                    "alarm4"            : $scope.DC02_GF4_CURR_ALARM4,
                                    "alarm5"            : $scope.DC02_GF4_CURR_ALARM5,
                                    "etatPompe1"        : $scope.DC02_CVC_PMA_GF4_RM,
                                    "etatPompe2"        : $scope.DC02_CVC_PMB_GF4_RM,                                          
                                    "alarmPompe1"       : $scope.DC02_CVC_PMA_GF4_DF,
                                    "alarmPompe2"       : $scope.DC02_CVC_PMB_GF4_DF,
                                    "debit"             : $scope.DC02_DEBIT_GF4                                        
                                  },
                        },
                        {
                          "name": "Groupe Froid 5B",
                          "value": "3",
                          "data": {
                                    "status"            : $scope.DC02_GF5_STATUS,
                                    "capacite"          : $scope.DC02_GF5_CAP_T,
                                    "alarm"             : $scope.DC02_GF5_ALM,
                                    "tmpEntreeEG"       : $scope.DC02_GF5_TMP_EE,    
                                    "tmpSortieEG"       : $scope.DC02_GF5_TMP_SE,
                                    "alarm1"            : $scope.DC02_GF5_CURR_ALARM1, 
                                    "alarm2"            : $scope.DC02_GF5_CURR_ALARM2,
                                    "alarm3"            : $scope.DC02_GF5_CURR_ALARM3,
                                    "alarm4"            : $scope.DC02_GF5_CURR_ALARM4,
                                    "alarm5"            : $scope.DC02_GF5_CURR_ALARM5,
                                    "etatPompe1"        : $scope.DC02_CVC_PMA_GF5_RM,
                                    "etatPompe2"        : $scope.DC02_CVC_PMB_GF5_RM,                                          
                                    "alarmPompe1"       : $scope.DC02_CVC_PMA_GF5_DF,
                                    "alarmPompe2"       : $scope.DC02_CVC_PMB_GF5_DF,
                                    "debit"             : $scope.DC02_DEBIT_GF5                                        
                                  },
                        }
                      ],
              }};

可以将此json存储在外部文件或服务中吗?

感谢您提供的帮助。

1 个答案:

答案 0 :(得分:0)

由于$ scope定义不是静态的(即它也引用了一些$ scope变量),实现你想要的可能方法是: 1)在某些文件中定义范围构造方法,如:

function createMyScope($scope){
$scope.template = .....
};

此方法将范围引用作为参数进行修改。 稍后您可以在控制器中使用它:

app.controller('test', function('$scope') {
   createMyScope($scope) ;
....

如果需要,您可以将范围创建者方法包装为某些异常常量或服务的一部分。