Environment: AngularJS application (using UI Router) that has nested states.
Short version: How do I change the value of a parent-state's dependency from inside a child's controller, such that siblings of the child state, who are also using the dependency will get the new value.
Long version: Take the following configuration:
shopApp.config(function($stateProvider) {
$stateProvider.state('shop', {
url : '/shop',
templateUrl : 'shop.html',
resolve : {
user : function($q) {
// callback that ends up returning null
// because the user is not logged in
}
},
controller: function($scope, user) {
// user is null at this point
$scope.user = user;
}
}).state('shop.login', {
url : '/login',
templateUrl : 'login.html',
controller : function($scope) {
// onLoggedIn is called from some form-processing
// logic once the user has successfully logged in
$scope.onLoggedIn = function(userObject) {
// I want the shop.userDetails state's controller
// to get the new $scope.user value
$scope.user = userObject;
$state.go('^.userDetails');
};
}
}).state('shop.userDetails', {
url : '/userDetails',
templateUrl : 'userDetails.html',
controller : function($scope, user) {
// Unfortunately user is null here
$scope.user = user;
}
});
});
The shop.login state's controller has a function called onLoggedIn that's called once the user has logged in successfully. An object containing user-information is passed to this function. From this point on I want all resolutions of the user dependency to use this value (i.e userObj). Unfortunately, instead of using the value that I've just assigned to $scope.user, it seems that the value that gets passed to the controller of shop.userDetails is the one that was originally resolved when the shop state's controller was created.
Does anyone know how to do this? If I'm doing this all wrong please tell me.