-1

I am trying to write an Angular code that will read constants from other URL and put those constants into current constant module.

Here is my sample Javascript:

var application = angular.module('mainApp', [])
.run(['$http', function($http){

alert('h1');

var gc;

var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
        console.log(this.response);
        window.gc = this.response;
    }
  };
  xhttp.open("GET", "myconst.properties", true);
  xhttp.send();

  alert(window.gc);


}]).controller('myController', function($scope, $rootScope) {
$scope.greetMessage = $rootScope.data;
});

Whatever I am using instead run like provider or config , alert(window.gc) always returning the undefined even if I'm using $http instead XMLHttpRequest, it remains the same. I am not using Angular constant because there is a huge number of constants are in there. I am avoiding that list of constants in my current script page.

Any suggestions would be appreciated.

kit
  • 1,166
  • 5
  • 16
  • 23
ARNAB2012
  • 300
  • 2
  • 5
  • 19

1 Answers1

0

used $rootScope directive here

var application = angular.module('mainApp', [])
.run(['$http','$rootScope', function($http,$rootScope){

alert('h1');

var gc;
$rootScope.gc = {};

var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
        console.log(this.response);
        window.gc = this.response;
        $rootScope.gc = this.response;
    }
  };
  xhttp.open("GET", "myconst.properties", true);
  xhttp.send();

  alert($rootScope.gc);

}])
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<div ng-app="mainApp">

</div>
Jignesh Joisar
  • 13,720
  • 5
  • 57
  • 57