1

How I spend a parameter title of the post, to display the browser's title?

I use pageTitle parameter on my route, but if put directly: slug as a value, not works.

.state('posts',{
    url         : '/blog/:slug',
    templateUrl : 'content/templates/single.html',
    controller  : 'SinglePostController',
    data: {
        pageTitle: 'title'
    },
    access: {
        requiredLogin: false
    }
})
1
  • Do you wanted to put slug on your browser title? but then what would be title for the other states? Commented Nov 1, 2015 at 20:23

1 Answer 1

2

The data : {} setting is static.

see similar:

If you want some dynamic feature use resolve : {}

Some links to examples and Q & A about resolve

EXTEND: A simple (really naive but working) example how to use resolve and $rootScope to manage browser title (check it here):

$stateProvider
  .state('home', {
      url: "/home",
      templateUrl: 'tpl.html',
      resolve: {
        'title': ['$rootScope', function($rootScope){
          $rootScope.title = "Other title";
        }],
      }
  })
  .state('parent', {
      url: "/parent",
      templateUrl: 'tpl.html',
      resolve: {
        'title': ['$rootScope', function($rootScope){
          $rootScope.title = "Title from Parent";
        }],
      }
  })
  .state('parent.child', { 
      url: "/child",
      templateUrl: 'tpl.html',
      controller: 'ChildCtrl',
      resolve: {
        'titleFromChild': ['$rootScope', function($rootScope){
          $rootScope.title = "Title from Child";
        }],
      }
  })

And this could be the html

<!DOCTYPE html>
<html ng-app="MyApp" ng-strict-di>

  <head>
    <title>{{title}}</title>

Try it here

A challenge here is - what to do on navigation from child to parent, but it could be done by moving that setting into controller and work with $scope.$on('detsroy'...

Here is adjusted plunker

.state('parent.child', { 
      url: "/child",
      templateUrl: 'tpl.html',
      controller: 'ChildCtrl',
      // no resolve, just controller fills the target
  })

.controller('ChildCtrl', ['$rootScope', '$scope', function ($rootScope, $scope) { 
  var title = $rootScope.title;
  $rootScope.title = "Title from Child";
  $scope.$on('$destroy', function(){$rootScope.title = title});
}])
Sign up to request clarification or add additional context in comments.

6 Comments

Okay, now step as the slug parameter to solve? Direct by stateParams? and set with rootScope?
@MarcoRiesco, if I do understand your issue, this should be the best asnwer for you: stackoverflow.com/q/30268800/1679310
my issue is on browser title
Browser title - the best way is $rootScope value.. changed in controller or resolve
Could you give me an example of how it's done in resolve, with rootscope?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.