2017-03-01 90 views
0

我正在用AngularJS寫WordPress。我做了一些頁面,我可以通過wp slug獲得。這是路由器的樣子:AngularJS ngRoute - 從模板請求獲取額外的數據

.when('/', { 
    template: '?async=1>', 
    controller: 'homeController', 
    controllerAs: 'works', 
}) 
.when('/works', { 
    template: 'works/?async=1>', 
    controller: 'worksController', 
    controllerAs: 'works', 
}) 
.when('/blog', { 
    template: 'blog/?async=1>', 
    controller: 'blogController', 
    controllerAs: 'blog', 
}) 

// etc 

我用async GET參數來表示,從wheter瀏覽器或角模板請求其正常的GET請求。

現在的問題是,我想獲取一些元數據時,請求像標題,元描述和其他一些模板。

也許使用一些自定義標題(如何獲取它們)?或者是否有可能以某種方式攔截響應,修改它並返回html進行渲染?

我知道,我可以使用隱藏的輸入,但它不是最好的練習。

回答

0

也許這不是最好的方法,但我找到了一個解決方案。

我使用自定義指令和鏈接我只是使用注入服務來傳播這些數據在我需要的地方。

myApp.directive("metaData", ['metaService', function(metaService) { 
    return { 
     restrict: 'E', 
     link: function(scope, element, attrs) { 
      metaService.set({ 
       title : attrs.title, 
       description: attrs.description 
      }); 
     } 
    } 
}]); 

和指令:

<meta-data data-title="The title" data-description="The description"></meta-data>