UI Router dynamic <title> tag

北城余情 提交于 2019-12-07 00:05:29

you have to use app.run() in your app.js file and assign your title in $rootScope.title . you can follow this code

app.run(function($rootScope){
    $rootScope.$on('$stateChangeStart', function(event, toState, toParams, fromState){

        $rootScope.title=toState.data.title;
    });
});

after this then bind the variable in your html like this

<title ng-bind="title"></title>

I think it will helpful

There is a working example

I would say, you are almost there. The title could look like this:

<title>{{$state.current.data.title}} {{$stateParams.ID}}</title>

Let's have these two states:

  .state('parent', {
      url: "/parent",
      templateUrl: 'tpl.html',
      data : { title: 'Title for PARENT' },
  })
  .state('parent.child', { 
      url: "/child/:ID",
      templateUrl: 'tpl.html',
      controller: 'ChildCtrl',
      data : { title: 'Title for CHILD' },
  })
  ;

And call them like this:

<a ui-sref="parent">
<a ui-sref="parent.child({ID:1})">
<a ui-sref="parent.child({ID:2})">

And with this hook:

.run(['$rootScope', '$state', '$stateParams',
  function ($rootScope, $state, $stateParams) {
    $rootScope.$state = $state;
    $rootScope.$stateParams = $stateParams;
}])

So, the point is, that in a $rootScope we can access both, $state.current and $stateParams

Check it here (NOTE, to see plunker in separated window, click the right -top corner blue icon - new window will change the title as well)

I'd suggest you to use params option instead of using data option, because params can be optional and you can set it dynamically by passing parameter inside your $state.go or ui-sref directive.

Code

.state('projects', {
    url: '/',
    templateUrl: 'projects/projects.html',
    ncyBreadcrumb: {
        label: 'Projects'
    },
    params: {
        title: { value: null }
    }
});

From Controller

$state.go('projects', {title: 'Page1'}); //you can change title while calling state

From HTML

ui-sref="projects({title: 'Something Else'})"
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!