Query : Want to access the Main Controller
scope object into ui-view
states
.
Problem Statement :
Here, I am creating one scope object($scope.myVar
) based on the response getting from the API that will be applicable across the application. hence, I wrote an API in Main Controller
to create this scope object as it is a parent
controller and all other states(ui-view
) are child
.
Here, I want to access that
$scope.myVar
in all the states in ui-view
.
Tried so far : HTML
<body ng-controller="MainController">
<div class="className">
<ui-view></ui-view>
</div>
</body>
Main Controller
app.controller('MainController', function($scope, $http) {
$http({
method: 'GET',
url: '/someUrl'
}).then(function successCallback(response) {
$scope.myVar = 'xyz'; // This variable i want to access into the state controller.
}, function errorCallback(response) {
});
});
State controller :
app.controller('StateController', function($scope, $timeout) {
console.log($scope.myVar); // undefined (Not working)
$timeout(function() {
console.log($scope.myVar); // xyz (working)
}, 500);
});
I want to access $scope.myVar
without using $timeout
service. What is the correct way to do this ?