我動態改變我的冠軍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-based-angularjs-partial-view –