marilyn marilyn - 2 months ago 7
AngularJS Question

angularjs $scope in json load in controller

I am looking to my json store that contains "$ scope" not initilaliser in an external file. Enssuite to load it into a controller. An example of json file.

$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
},
}
],
}};


it is possible to store this json in an external file or a service?

Thank you for the help provided.

Answer

Since your $scope definition is not static (i.e. it has references to some $scope variables as well), the possible way to achieve what you want is to: 1) define your scope constructing method in some file like:

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

This method takes scope reference as a parameter and modifies it. Later you can use it in your controller as:

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

You can wrap the scope creator method to be part of some anjular constant or service if you want.

Comments