2012-12-26 7 views

答えて

42

ほとんどどこでもあなたのクライアント側のJavaScriptコード内:

UI.registerHelper "setTitle", -> 
    title = "" 
    for i in [0..arguments.length-2] 
    title += arguments[i] 
    document.title = title 
    undefined 

やJsの中に同じ:あなたはタイトル(のCoffeeScript)を設定するためのヘルパーを作成することができます

document.title = "My new title"; 
+2

ありがとう、ありがとう。 –

10

UI.registerHelper("setTitle", function() { 
    var title = ""; 
    for (var i = 0; i < arguments.length - 1; ++i) { 
    title += arguments[i]; 
    } 
    document.title = title; 
}); 

複雑な方法で使用すると反応します:

{{#if book}} 
    {{setTitle book.title}} 
{{else}} 
    {{setTitle "My books"}} 
{{/if}} 
37

あなたはデビッドWihlのソリューション拡張することができます。そして、あなたはいつでもコールにすることができます

Deps.autorun(function(){ 
    document.title = Session.get("DocumentTitle"); 
}); 

を:

Session.set("DocumentTitle","New Document Title"); 
+0

この回答をLove! –

+0

https://github.com/oortcloud/unofficial-meteor-faq#how-do-i-get-reactive-html-in-the-head-tag – jefeman

7

私はルータに直接の事のようなものを処理するために、それはより便利見つけますonBeforeAction

Router.map(function() { 
    return this.route('StudioRoot', { 
    path: '/', 
    onBeforeAction: function() { 
     return document.title = "My Awesome Meteor Application"; 
    } 
    }); 
}); 
4

テンプレート内に存在しない<head> </head>のタグに含まれています。あなたはメタタグと一緒にタイトルを処理するためのパッケージmanuelschoebel:ms-seoを追加することができますiron:router使用する場合

<head> 
    <title>my title</title> 
</head> 

<body> 
    ... 
</body> 

<template name="mytemplate"> 
    ... 
</template> 
12

:sample.htmlの

内容:これを試してみてください。 Meteor.isClientで

::私がやってしまった何を

Router.map(function() { 
    return this.route('blogPost', { 
    path: '/blog/:slug', 

    onAfterAction: function() { 
     var post = this.data().post; 
     SEO.set({ 
     title: post.title, 
     meta: { 
      'description': post.description 
     }, 
     og: { 
      'title': post.title, 
      'description': post.description 
     } 
     }); 
    } 
    }); 
}); 
1

:今VARが反応的に設定されていることを

Meteor.startup(function() { 
    Deps.autorun(function() { 
     document.title = Session.get('documentTitle'); 
    }); 
}); 

が、中に行くこれは、静的および動的なSEOデータに便利ですルータファイル(まだ実行されていない場合:meteor add iron:router。ルータファイルはクライアントとサーバの両方です)

Router.route('/', { 
    name: 'nameOfYourTemplate', 
    onBeforeAction: function() { 
     Session.set('documentTitle', 'whateverTitleIWant'); 
     this.next(); //Otherwise I would get no template displayed 
    } 
}); 

It既にheadタグにタイトルを設定していても問題ありません。これは、あなたのルートに従ってこのものに置き換えられます。

+0

いくつかの問題はありませんが、セッション変数ページをリフレッシュしても生き残ることはできません。 –

+0

@Durham、リフレッシュでルートは再び実行されます。 – BudgieInWA

-1

私はui-routerでうまくいく答えを探す必要がありました。私はこれがあなたが探していた答えではないかもしれないことを知っています。この質問は、約2年前に投稿されましたので、私は他の誰かがUI-ルータと解決策を探してここに来たならば、この答えは彼らを助けることができる考え出し:

myApp.run.js

(function() { 
    'use strict'; 

    angular 
    .module('myApp') 
    .run(run); 

    run.$inject = ['$rootScope', '$state']; 

    function run($rootScope, $state) { 
    $rootScope.$on("$stateChangeSuccess", function(previousRoute, currentRoute){ 
     document.title = 'myApp - ' + currentRoute.data.pageTitle; 
    }); 
    }; 

})(); 

ルート。js

(function() { 
    'use strict'; 

    angular 
     .module('myApp') 
     .config(config); 

    config.$inject = 
     ['$urlRouterProvider', '$stateProvider', '$locationProvider']; 

    function config($urlRouterProvider, $stateProvider) { 

     // ... 
     $stateProvider 
      .state('home', { 
      url: '/', 
      templateUrl: 'client/home/views/home.ng.html', 
      controller: 'HomeController', 
      data: { 
       pageTitle: 'My Dynamic title' 
      } 
      }) 
    } 
})(); 
+0

なぜこれが控除されたのですか? –

+0

Angular.jsの例をMeteor.js関連の質問スレッドに投稿したためです。 –

関連する問題