私は次のコードを使用して、AngularJsで動的に私のタイトルを変更しています:Angular Jsで変数を使用してページタイトルを変更するにはどうすればよいですか?
app.run(['$rootScope', function($rootScope) {
$rootScope.$on('$routeChangeSuccess', function (event, current, previous) {
$rootScope.title = current.$$route.title;
});
}]);
app.config(['$routeProvider', function($routeProvider) {
$routeProvider
.when('/', {
title: 'My Tagline',
controller : 'IndexController',
templateUrl : 'static/app_partials/index.html'
})
.when('/categories/:categoryPk/', {
title: $scope.category.name,
controller : 'CategoryController',
templateUrl : 'static/app_partials/category.html'
})
.otherwise({ redirectTo : '/' });
}]);
Htmlの
<title ng-bind="'My Brand - ' + title">My Brand</title>
ので、IndexController
ため、タイトルは単純な文字列なので、この方法であり、うまく動作します。しかし、私のCategoryController
の場合は、コントローラコードで初期化する変数、具体的には$scope.category.name
からタイトルを生成します。これどうやってするの?どんな助け?次のように
ます。また、ここで答えを見つけることができます。https://stackoverflow.comを/ questions/12506329/how-to-dynamic-change-header-on-angularjs-partial-view –