I implemented a custom page title according to ngBoilerplate:
https://github.com/ngbp/ngbp/blob/v0.3.1-release/src/app/app.js
.controller( 'AppCtrl', function AppCtrl ( $scope, $location ) {
$scope.$on('$stateChangeSuccess', function(event, toState, toParams, fromState, fromParams){
if ( angular.isDefined( toState.data.pageTitle ) ) {
$scope.pageTitle = toState.data.pageTitle + ' | ngBoilerplate' ;
}
});
})
So basically, the title changes on each $stateChangeSuccess
event. The problem is, that in my browser history, it saves the new title as the title of the last page. So for instance if I am at a page with the title "Home" and navigate to a page with the title "About", my history will show "About" as last item, but navigate correctly to "Home" when I click on it. I fear that this will really confuse people.
So I came up with a "hack", that seems to solve this problem:
.controller( 'AppCtrl', function AppCtrl ( $scope, $location ) {
var nextTitle = "";
$scope.$on('$locationChangeSuccess', function (event, newUrl, oldUrl) {
$scope.pageTitle = nextTitle;
});
$scope.$on('$stateChangeSuccess', function(event, toState, toParams, fromState, fromParams){
if ( angular.isDefined( toState.data.pageTitle ) ) {
nextTitle = toState.data.pageTitle + ' | ngBoilerplate' ;
}
});
})
So now, the title will be save in a variable first and will be assigned to the scope only on $locationChangeSuccess
. This seems to work, but I don't know how reliable this is.
So my question is, I guess, how to do this correctly. How do I get a reliable changing page title, that gets it's data from the current state?
And furthermore out of interest: Why doesn't it work with $stateChangeSuccess
? Is there a reason why this event fires so early?