gpt4 book ai didi

angularjs - Angular 元数据服务 [$injector :unpr]

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:40:14 27 4
gpt4 key购买 nike

我正在为我的网站和 SEO 优化编写元数据服务,但似乎我无法克服错误。我不确定为什么。我在 head 标签中插入了我的 html Controller 标签
ng-controller="MetaDataCtrl"。我是 Angular 的初学者,我正在从在线教程中抓取。

我在以下位置找到此代码:dynamic metadata in angular

Main.js

    var app = angular.module('WebApp', [
'ngRoute'
]);
angular.module("WebApp").service("metadataService" ["$location", "$rootScope", function($location, $routescope) {
var self = this;
self.tags = {};

// Set custom options or use provided fallback (default) options
self.loadMetadata = function loadMetadata(metadata) {
metadata = metadata || {};
metadata.title = metadata.title || 'BNL Consulting';
metadata.description = metadata.description || 'We are BNL Consulting.';
document.title = metadata.title;
self.tags = metadata;
};

// Make sure data gets reloaded when navigation occurs
$rootScope.$on('$routeChangeSuccess', function (event, newroute, oldroute) {
self.loadMetadata(newroute.metadata);
});
}
]),

/**
* Configure the Routes
*/
app.config(['$routeProvider', '$locationProvider', function($routes, $location) {
$location.html5Mode(true).hashPrefix('!');
$routes
// Home
.when("/", {templateUrl: "partials/home.html",
controller: "PageCtrl",
metadata: {
title: 'This is my title',
description: 'This is Desc.' }

})
}]);

app.controller('PageCtrl', function (/* $scope, $location, $http */) {

});

.controller('MetadataCtrl', function ($scope, metadataService) {
$scope.meta = metadataService;
});

最佳答案

这是正确的代码,如您链接的文章 (here) 中所示。我写了这篇文章,它对我来说是无缝的。这还包括硬编码的回退标记,以防爬虫未拾取 Javascript。

注意:这些不是完整的文件,只是相关的重要部分。如果您需要帮助编写可以在别处找到的指令、服务等的样板。无论如何,这里......

app.js

这是您为每条路线(标题、描述等)提供自定义元数据的地方

$routeProvider
.when('/', {
templateUrl: 'views/homepage.html',
controller: 'HomepageCtrl',
metadata: {
title: 'The Base Page Title',
description: 'The Base Page Description' }
})
.when('/portfolio', {
templateUrl: 'views/portfolio.html',
controller: 'PortfolioCtrl',
metadata: {
title: 'The Portfolio Page Title',
description: 'The Portfolio Page Description' }
})

metadata-service.js(服务)

设置自定义元数据选项或使用默认值作为后备。

var self = this;

// Set custom options or use provided fallback (default) options
self.loadMetadata = function(metadata) {
self.title = document.title = metadata.title || 'Fallback Title';
self.description = metadata.description || 'Fallback Description';
self.url = metadata.url || $location.absUrl();
self.image = metadata.image || 'fallbackimage.jpg';
self.ogpType = metadata.ogpType || 'website';
self.twitterCard = metadata.twitterCard || 'summary_large_image';
self.twitterSite = metadata.twitterSite || '@fallback_handle';
};

// Route change handler, sets the route's defined metadata
$rootScope.$on('$routeChangeSuccess', function (event, newRoute) {
self.loadMetadata(newRoute.metadata);
});

metaproperty.js(指令)

打包 View 的元数据服务结果。

return {
restrict: 'A',
scope: {
metaproperty: '@'
},
link: function postLink(scope, element, attrs) {
scope.default = element.attr('content');
scope.metadata = metadataService;

// Watch for metadata changes and set content
scope.$watch('metadata', function (newVal, oldVal) {
setContent(newVal);
}, true);

// Set the content attribute with new metadataService value or back to the default
function setContent(metadata) {
var content = metadata[scope.metaproperty] || scope.default;
element.attr('content', content);
}

setContent(scope.metadata);
}
};

index.html

<head>
<title>Fallback Title</title>
<meta name="description" metaproperty="description" content="Fallback Description">

<!-- Open Graph Protocol Tags -->
<meta property="og:url" content="fallbackurl.com" metaproperty="url">
<meta property="og:title" content="Fallback Title" metaproperty="title">
<meta property="og:description" content="Fallback Description" metaproperty="description">
<meta property="og:type" content="website" metaproperty="ogpType">
<meta property="og:image" content="fallbackimage.jpg" metaproperty="image">

<!-- Twitter Card Tags -->
<meta name="twitter:card" content="summary_large_image" metaproperty="twitterCard">
<meta name="twitter:title" content="Fallback Title" metaproperty="title">
<meta name="twitter:description" content="Fallback Description" metaproperty="description">
<meta name="twitter:site" content="@fallback_handle" metaproperty="twitterSite">
<meta name="twitter:image:src" content="fallbackimage.jpg" metaproperty="image">
</head>

关于angularjs - Angular 元数据服务 [$injector :unpr],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33522148/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com