Skip to content

Instantly share code, notes, and snippets.

@chekit
Last active July 5, 2016 07:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chekit/f4b74becb3f70afc16586c727d8e8bc9 to your computer and use it in GitHub Desktop.
Save chekit/f4b74becb3f70afc16586c727d8e8bc9 to your computer and use it in GitHub Desktop.
set <title> with angularJS

Данное решение необходимо для динамического изменения title страницы по средствам AngularJS

HTML

...
<title ng-bind="'Титул' + title"></title>
<!-- ng-bind вместо {{}} нужен для того, чтобы в title не "мигал" expression-->
...

JS

angular.module('myApp', [])
  .config(['$routeProvider', $routeProvider => {
    $routeProvider
      .when('/', {
        title: 'Тайтл для страницы',
        templateUrl: 'views/index.html',
        controller: 'IndexController',
        controllerAs: 'indexCtrl',
        pageName: 'index'
      })
      .otherwise({
        redirectTo: '/'
      });
  }])
  .run('$rootScope', $rootScope => {
    $rootScope.$on(['$routeChangeSuccess', (evt, curr) => {
      //Так же $rootScope.title можно устанавливать непосредственно из контроллера страницы
      $rootScope.title = curr.$$route.title.length > 0 ? curr.$$route.title : '';
    }]);
  });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment