Global Variable From Inner Function - Javascript [duplicate]
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
This question already has an answer here:
Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference
6 answers
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.
javascript angularjs global-variables
marked as duplicate by CertainPerformance
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Nov 15 '18 at 7:26
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference
6 answers
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.
javascript angularjs global-variables
marked as duplicate by CertainPerformance
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Nov 15 '18 at 7:26
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
used$rootScope
directive in xmlHttpRequest
– Jignesh Joisar
Nov 15 '18 at 7:15
1
the HTTP module is asynchronous which mean your code still runs while the network request happens,window.gc
only exists after the call to onreadystatechanage has been made.
– dotconnor
Nov 15 '18 at 7:20
add a comment |
This question already has an answer here:
Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference
6 answers
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.
javascript angularjs global-variables
This question already has an answer here:
Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference
6 answers
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.
This question already has an answer here:
Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference
6 answers
javascript angularjs global-variables
javascript angularjs global-variables
edited Nov 15 '18 at 8:20
kit
1,10631017
1,10631017
asked Nov 15 '18 at 7:13
ARNAB2012ARNAB2012
1611417
1611417
marked as duplicate by CertainPerformance
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Nov 15 '18 at 7:26
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by CertainPerformance
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
Nov 15 '18 at 7:26
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
used$rootScope
directive in xmlHttpRequest
– Jignesh Joisar
Nov 15 '18 at 7:15
1
the HTTP module is asynchronous which mean your code still runs while the network request happens,window.gc
only exists after the call to onreadystatechanage has been made.
– dotconnor
Nov 15 '18 at 7:20
add a comment |
used$rootScope
directive in xmlHttpRequest
– Jignesh Joisar
Nov 15 '18 at 7:15
1
the HTTP module is asynchronous which mean your code still runs while the network request happens,window.gc
only exists after the call to onreadystatechanage has been made.
– dotconnor
Nov 15 '18 at 7:20
used
$rootScope
directive in xmlHttpRequest– Jignesh Joisar
Nov 15 '18 at 7:15
used
$rootScope
directive in xmlHttpRequest– Jignesh Joisar
Nov 15 '18 at 7:15
1
1
the HTTP module is asynchronous which mean your code still runs while the network request happens,
window.gc
only exists after the call to onreadystatechanage has been made.– dotconnor
Nov 15 '18 at 7:20
the HTTP module is asynchronous which mean your code still runs while the network request happens,
window.gc
only exists after the call to onreadystatechanage has been made.– dotconnor
Nov 15 '18 at 7:20
add a comment |
1 Answer
1
active
oldest
votes
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>
Its remains the same. $rootScope.gc object is empty
– ARNAB2012
Nov 15 '18 at 8:47
i think your response is empty
– Jignesh Joisar
Nov 15 '18 at 9:05
No. Its not. The response has some data.
– ARNAB2012
Nov 15 '18 at 17:04
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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>
Its remains the same. $rootScope.gc object is empty
– ARNAB2012
Nov 15 '18 at 8:47
i think your response is empty
– Jignesh Joisar
Nov 15 '18 at 9:05
No. Its not. The response has some data.
– ARNAB2012
Nov 15 '18 at 17:04
add a comment |
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>
Its remains the same. $rootScope.gc object is empty
– ARNAB2012
Nov 15 '18 at 8:47
i think your response is empty
– Jignesh Joisar
Nov 15 '18 at 9:05
No. Its not. The response has some data.
– ARNAB2012
Nov 15 '18 at 17:04
add a comment |
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>
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>
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>
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>
answered Nov 15 '18 at 7:21
Jignesh JoisarJignesh Joisar
3,09021125
3,09021125
Its remains the same. $rootScope.gc object is empty
– ARNAB2012
Nov 15 '18 at 8:47
i think your response is empty
– Jignesh Joisar
Nov 15 '18 at 9:05
No. Its not. The response has some data.
– ARNAB2012
Nov 15 '18 at 17:04
add a comment |
Its remains the same. $rootScope.gc object is empty
– ARNAB2012
Nov 15 '18 at 8:47
i think your response is empty
– Jignesh Joisar
Nov 15 '18 at 9:05
No. Its not. The response has some data.
– ARNAB2012
Nov 15 '18 at 17:04
Its remains the same. $rootScope.gc object is empty
– ARNAB2012
Nov 15 '18 at 8:47
Its remains the same. $rootScope.gc object is empty
– ARNAB2012
Nov 15 '18 at 8:47
i think your response is empty
– Jignesh Joisar
Nov 15 '18 at 9:05
i think your response is empty
– Jignesh Joisar
Nov 15 '18 at 9:05
No. Its not. The response has some data.
– ARNAB2012
Nov 15 '18 at 17:04
No. Its not. The response has some data.
– ARNAB2012
Nov 15 '18 at 17:04
add a comment |
used
$rootScope
directive in xmlHttpRequest– Jignesh Joisar
Nov 15 '18 at 7:15
1
the HTTP module is asynchronous which mean your code still runs while the network request happens,
window.gc
only exists after the call to onreadystatechanage has been made.– dotconnor
Nov 15 '18 at 7:20