1) I have variables initialized in ng-init Eg -
ng-init="password='Mightybear'";
2) I want to access it from the .run method. Eg -
anguar.module("ngApp", [])
.run(function() {
//Access password here
});
Below scenarios I have tried, and did not work -
1) angular.module("ngApp", [])
.run(function($rootScope) {
console.log($rootScope.password) //undefined!!!
});
2) angular.module("ngApp", [])
.run(function($rootScope, $timeout) {
$(timeout(function() {
console.log($rootScope.password) //undefined!!!
});
});
You can not get ng-init value inside your
runblockAngular LifeCycle
ng-initis here)If you want to get initialize value in run phase then you need to set that value in config phase.
If You want to set the value in config then you can take use of
app.constant/providerwhich available in configuration phase, don't use$rootScopethat is considered as bad pattern in AngularJS.Code