5

この答えは、別の SO の質問で見つけました。しかし、一部のページ タイトルでもルート パラメータを使用したいと考えています。

私が$routeProviderこのようなものを持っている場合:

$routeProvider.when('/demo/:name/:idNumber', {title : ' - :name', templateUrl: 'partials/details.html', controller: 'AppDetails'}); 

:nameタイトルと場所を一致させるにはどうすればよい:nameですか? 私は次のことを試しました

.run(['$location', '$rootScope', '$routeParams', function($location, $rootScope,   $routeParams) {
  $rootScope.$on('$routeChangeSuccess', function (event, current, previous) {
    if(current.$$route.title.contains(":")){
        //Not sure how to get the route param using the title param string
    }else{
      $rootScope.title = current.$$route.title;
    }
  });
}]);

しかし、文字列を使用して routeParams から変数を取得する方法がわかりません。

4

3 に答える 3

2

次のようにアクセスできませんか?

$routeParams[$routeParams.title]

それがあなたが求めていることだと思いますが、よくわかりません...

于 2014-01-16T16:10:49.233 に答える
2

追加した routeChangeSuccess ハンドラーで

var title = currentRoute.$$route.title;
if (title && $routeParams){
    for(var paramName in $routeParams) {
        title = title.replace(new RegExp(':' + paramName, 'g'), $routeParams[paramName]);
    }
}
$rootScope.title = title;
于 2014-07-23T19:07:30.317 に答える
0

このようなタイトルのタイプを確認するようにランナーを変更します。

    .run(['$rootScope', '$route', '$routeParams', function($rootScope, $route, $routeParams) {
    $rootScope.$on('$routeChangeSuccess', function() {
       if(!!$route.current.title) {
           if(typeof $route.current.title == 'function') {
               document.title = $route.current.title($routeParams);
           } else if (typeof $route.current.title == 'string') {
               document.title = $route.current.title;
           }

       }
    });
}]);

そして、タイトルを作成したいので、 routeProvider で関数を定義します。

$routeProvider.when('/demo/:name/:idNumber', {
     title : function(params){ return "Name: " + params.name;},
     templateUrl: 'partials/details.html', 
     controller: 'AppDetails'}); 
于 2015-11-23T12:14:00.843 に答える