I’m writing metadata service for my website and SEO optimisaion and it seems i cant get past the error. I’m not sure why. I have my html controller tag inserted in head tag
ng-controller="MetaDataCtrl"
. I’m beginer in Angular and I’m scraping from on line tutorials.
I found this code in: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;
});
3
Answers
I solved this problem and i wanna share for other users. I tried this way based on article for SEO optimization but it doesn't work google bots still see {{meta.tags.title }} and not Some dynamic text. I didnt write my services in the right way so the error keep poping when i wrote ng-controller. If you services are working fine tag has no problem with angularJs tags
I believe it’s because your tag is on the head tag which seems to be a special case. I believe your question will be answered by this.
This is the proper code, as shown in the article you linked (here). I wrote the article, and it worked seamlessly for me. This also includes hardcoded fallback tags in case Javascript isn’t picked up by the crawler.
Note: these aren’t the complete files, just the important parts of those that are relevant. If you need help writing the boilerplate for directives, services, etc. that can be found elsewhere. Anyway, here goes…
app.js
This is where you provide the custom metadata here for each of your routes (title, description, etc.)
metadata-service.js (service)
Sets the custom metadata options or use defaults as fallbacks.
metaproperty.js (directive)
Packages the metadata service results for the view.
index.html