I'm working on an Angular project and currently am working on making the entire project configurable. I have an object which contains an array of elements, each element being an object with the details of different page of the project.
Now I'm able to get the object when the project loads but I'm stuck at the navigation part. Previously, I had passed a text in ui-sref
and state
file and on clicking a link the page got navigated. But now the text would be configurable, i.e editable by the user. So I passed that variable (the text entered by the user) in ui-sref
and in the controller
file. I entered the title of all the array elements to an array defined with $rootScope by iterating in the controller
and then used this array variable in the state
file as $rootScope.array[0]
.
So, is this method correct? I'm getting the following error now on loading the project -
Uncaught Error: [$injector:modulerr] Failed to instantiate module myModule due to: Error: [$injector:unpr] Unknown provider: $rootScope
Now i get that its saying $rootScope
as unknown provider, but how to solve that?
I've passed $rootScope
as argument in all the angular files that are of concern here.
Also - can a $rootScope variable be passed in the state
file?
EDIT - here's the controller
and state
code for some information -
Controller - C:\sandboxes\projects\Tests\demo\core\src\main\webapp\app\layouts\navigate
$scope.getData = function() {
return $http.get('api/configurations')
.success(function (response) {
$scope.JsonData = response;
return response;
})
.error(function (data, status, header, config) {
$scope.ResponseDetails = "Data: " + data +
"<br />status: " + status;
});
}
$scope.getData().then(function(response){
console.log(response);
console.log($scope.JsonData[0].appConfigInfo);
$scope.configObjectJson = JSON.parse($scope.JsonData[0].appConfigInfo); // appConfigInfo contains the data of the different pages
$scope.configObject = $scope.configObjectJson.PageConfig;
console.log($scope.configObject);
// below array to store the value of the title of each element of the array
$rootScope.titleArray = [];
for(var i=0; i < $scope.configObject.dashboards.length; i++) {
$rootScope.titleArray[i] = $scope.configObject.dashboards[i].title;
}
console.log($rootScope.titleArray);
});
State - C:\sandboxes\projects\Tests\demo\core\src\main\webapp\app\pages\loading-page
angular.module('myApp')
.config(stateConfig);
stateConfig.$inject = ['$stateProvider', '$rootScope'];
function stateConfig($stateProvider, $rootScope) {
$stateProvider
.state($rootScope.titleArray[0], { // $rootScope.titleArray is the variable created in the above controller
parent: 'home',
url: 'loadingPage',
data: {
authorities: ['ROLE_USER'],
pageTitle: $rootScope.titleArray[0]
},
views: {
'appContent@home': {
templateUrl: 'app/entities/loading-page/loadingPage.html',
controller: 'LoadingPageController',
controllerAs: 'vm'
}
}
})
}
Note - the controller
and state
files are at different locations under main folder.
I hope the question is clear enough. Any helpful comment is much appreciated. Thanks.