我正试图找到一些方法来获取$ routeParams中页面的标题。我问你有关如何正确配置的任何建议。
在index.html中
<head>
<title>{{title}} - domain.com</title>
</head>
app.js
var myApp = angular.module('tareasApp', ['ngRoute']);
myApp.config(function ($routeProvider, $locationProvider) {
$locationProvider.html5Mode(true);
$routeProvider
.when("/:pageName", {
templateUrl: "views/wizard.html",
title:":PageName",
controller: "MainCtrl"
})
.otherwise({
redirectTo: '/'
});
});
myApp.controller('MainCtrl', function ($scope, $routeParams, $location) {
$scope.pageName = $routeParams.pageName;
});
我使用了这段代码,但没有成功。如何正确调整以在$ routeParams中工作?
myApp.run(['$location', '$rootScope', '$routeParams', function($location, $rootScope, $routeParams) {
$rootScope.$on('$routeChangeSuccess', function (event, current, previous) {
var title = current.$$route.title;
if (title && $routeParams){
for(var PageName in $routeParams) {
title = title.replace(new RegExp(':' + PageName, 'g'), $routeParams[PageName]);
}
}
$rootScope.title = title;
});
}]);