【发布时间】:2018-03-24 01:20:00
【问题描述】:
由于某种原因,我无法为 head 元素中的元素属性进行绑定。令人惊讶的是,相同的绑定确实适用于 head 元素中的元素。
我在下面粘贴了我正在使用的代码。 title 元素被正确绑定,但相同的绑定不适用于 meta property="og:title" 元素。我已经尝试过 content="{{ var }}" 和 ng-attr-content="{{ var }}"。谁能告诉我为什么会这样以及如何解决这个问题?
我在 chrome 开发者控制台中看到以下内容:
index.html:
<!DOCTYPE html>
<html ng-app="test">
<head>
<title ng-bind="documentTitle"></title>
<meta property="og:title" ng-attr-content="{{ documentTitle }}" />
<meta name="description" ng-if="documentDescription" ng-attr-content="{{ documentDescription }}" />
<meta name="fragment" content="!">
<meta name="prerender-status-code" ng-attr-content="{{statusCode}}">
<base href="/" />
{% include 'stylesheets.html' %}
</head>
<body>
{% include 'javascripts.html' %}
</body>
</html>
test.js:
function run($http, $locale, $rootScope, $route, Head) {
$rootScope.$on('$routeChangeSuccess', function() {
Head.setTitle($route.current.meta.title);
Head.setDescription($route.current.meta.description);
Head.setStatusCode('200');
});
}
头部服务:
(function ($, _) {
'use strict';
angular
.module('test.utils.services')
.factory('Head', Head);
Head.$inject = ['$rootScope'];
function Head($rootScope) {
var Head = {
setStatusCode: setStatusCode,
setTitle: setTitle,
setDescription: setDescription
};
return Head;
function setStatusCode(statusCode) {
$rootScope.statusCode = statusCode;
}
function setTitle(title) {
$rootScope.documentTitle = title;
}
function setDescription(description) {
$rootScope.documentDescription = description;
}
}
})($, _);
【问题讨论】:
标签: javascript html angularjs angularjs-scope