I have utilized the Value() Module in my AngularJS App. I have declared and modified the Value, but I am not able to retrieve the modified or latest Value in a Controller.
Flow is as follows:
- STEP 1: Initialize the Value with .value("key": "value")
- STEP 2: Modify the Value in RUN Block. If I do console.log() in this STEP, I am getting the desired output. That means the Value is getting updated.
- STEP 3: Access that modified or latest value in Controller for further process.
I have mentioned the STEPS in the Code below.
app.module.js
(function () {
"use strict";
angular
.module("app", [
"app.authentication"
])
//STEP 1: Initialize the Value.
.value("errorCollectionObject", {})
.run(getErrorCollection)
function getErrorCollection($rootScope, $location, $http, errorCollectionObject) {
$rootScope.$on('$routeChangeStart', function (event, next, current) {
$http.get("../app/core/error-collection.json")
.then(function (response) {
//STEP 2: Modify/Update the Value.
errorCollectionObject = response.data;
});
});
}
})();
login.controller.js
(function () {
"use strict";
angular
.module("app.authentication")
.controller("LoginController", LoginController)
LoginController.$inject = ["errorCollectionObject"];
function LoginController(errorCollectionObject) {
var vm = this;
//STEP 3: Use the Modified or Latest Value
//But instead of getting the Modified value, I am getting the null Object {} that I initialized in STEP 1.
console.log(errorCollectionObject);
//Output is: {}
}
})();
Any idea what is going wrong? Or am I implementing the concept in a wrong way? Any help will be greatly appreciated.