Angularjs ngroute-从模板请求中获取其他数据



我正在用AngularJS在WordPress上写一个门户。我制作了一些页面,可以通过wp slug获得。这就是路由器的样子:

.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请求。

现在问题是,我想在请求标题,元描述等模板时获取一些元数据。

也许使用一些自定义标头(如何以这种方式获取它们)?还是可以以某种方式拦截响应,更改它并返回HTML进行渲染?

我知道,我可以使用隐藏的输入,但这不是最佳实践。

也许不是最好的方法,但是我找到了一个解决方案。

我使用自定义指令并链接时,我只使用注射服务将这些数据传播到我需要的地方。

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>

最新更新