【发布时间】:2017-10-28 07:01:16
【问题描述】:
我正在使用以下代码在 AngularJs 中动态更改我的标题:
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,我在控制器代码中对其进行了初始化。我怎样才能做到这一点?有什么帮助吗?
【问题讨论】:
-
你也可以在这里找到答案:stackoverflow.com/questions/12506329/…
标签: javascript angularjs angular-routing