angular-update-meta
Advanced tools
Comparing version 2.0.1 to 2.1.0
{ | ||
"name": "angular-update-meta", | ||
"version": "2.0.1", | ||
"version": "2.1.0", | ||
"authors": [ | ||
@@ -5,0 +5,0 @@ { |
@@ -159,2 +159,30 @@ // Create all modules and define dependencies to make sure they exist | ||
(function () { | ||
function UpdateScriptDirective($log, updateScriptContent) { | ||
return { | ||
restrict: 'E', | ||
scope: { | ||
type: '@', | ||
content: '=' | ||
}, | ||
link: function (scope, iElem, iAttrs) { | ||
var selector = 'script[type="' + scope.type + '"]'; | ||
scope.$watch('content', function (newValue, oldValue) { | ||
if (typeof newValue !== 'undefined') { | ||
updateScriptContent.update(selector, scope.content); | ||
} | ||
}); | ||
} | ||
}; | ||
} | ||
// Inject dependencies | ||
UpdateScriptDirective.$inject = ['$log', 'updateScriptContent']; | ||
// Export | ||
angular | ||
.module('updateMeta') | ||
.directive('updateScript', UpdateScriptDirective); | ||
})(); | ||
(function(){ | ||
@@ -235,1 +263,38 @@ | ||
})(); | ||
(function () { | ||
/** | ||
* Service to update script content | ||
* | ||
* @constructor | ||
*/ | ||
function UpdateScriptContentService($log, $sce, $filter) { | ||
return { | ||
update: function (selector, content) { | ||
if (!document) { | ||
$log.error('updateMeta: document is not available!'); | ||
return; | ||
} | ||
if (!selector) { | ||
$log.error('updateMeta: please provide a selector'); | ||
return; | ||
} | ||
var element = document.querySelector(selector); | ||
if(element){ | ||
var trustedContent = $sce.trustAsHtml($filter('json')(content)); | ||
element.outerHTML = '<script type="application/ld+json">' + trustedContent + '</script>'; | ||
} | ||
} | ||
}; | ||
} | ||
// Inject dependencies | ||
UpdateScriptContentService.$inject = ['$log', '$sce', '$filter']; | ||
angular | ||
.module('updateMeta') | ||
.factory('updateScriptContent', UpdateScriptContentService); | ||
})(); |
@@ -1,1 +0,1 @@ | ||
angular.module("updateMeta",[]),function(){function t(t,e){function n(t,e){return"["+e+'="'+t[e]+'"]'}function r(t,e){return e.filter(function(e){return t[e]}).map(function(e){return n(t,e)}).join("")}var i=["href","charset","crossorigin","hreflang","integrity","media","methods","referrerpolicy","sizes","target","title","type"],u=["id","rel"];return{restrict:"E",scope:{rel:"@",href:"@",id:"@?",charset:"@?",crossorigin:"@?",hreflang:"@?",integrity:"@?",media:"@?",methods:"@?",referrerpolicy:"@?",sizes:"@?",target:"@?",title:"@?",type:"@?"},link:function(t){var n="link"+r(t,u);i.filter(function(e){return t[e]}).forEach(function(r){t.$watch(r,function(i){"undefined"!=typeof i&&e.update(n,r,t[r])})})}}}t.$inject=["$log","updateAttribute"],angular.module("updateMeta").directive("updateLink",t)}(),function(){function t(t,e){return{restrict:"E",scope:{charset:"@",name:"@",content:"@",httpEquiv:"@",scheme:"@",property:"@",itemprop:"@"},link:function(t){var n;t.name&&(n='meta[name="'+t.name+'"]'),t.httpEquiv&&(n='meta[http-equiv="'+t.httpEquiv+'"]'),t.property&&(n='meta[property="'+t.property+'"]'),t.itemprop&&(n='meta[itemprop="'+t.itemprop+'"]'),t.$watch("content",function(r){"undefined"!=typeof r&&e.update(n,"content",t.content)}),t.$watch("charset",function(n){"undefined"!=typeof n&&e.update("meta[charset]","charset",t.charset)})}}}t.$inject=["$log","updateAttribute"],angular.module("updateMeta").directive("updateMeta",t)}(),function(){function t(){return{restrict:"E",scope:{title:"@"},link:function(t){t.$watch("title",function(t){"undefined"!=typeof t&&document&&(document.title=t)})}}}t.$inject=["$log"],angular.module("updateMeta").directive("updateTitle",t)}(),function(){function t(t){function e(e,n,r){if(!document)return t.error("updateMeta: document is not available!"),void 0;if(!e)return t.error("updateMeta: please provide a selector"),void 0;var i=document.querySelector(e);i&&i.setAttribute&&i.setAttribute(n,r)}return{update:e}}t.$inject=["$log"],angular.module("updateMeta").factory("updateAttribute",t)}(); | ||
angular.module("updateMeta",[]),function(){function t(t,e){function n(t,e){return"["+e+'="'+t[e]+'"]'}function r(t,e){return e.filter(function(e){return t[e]}).map(function(e){return n(t,e)}).join("")}var i=["href","charset","crossorigin","hreflang","integrity","media","methods","referrerpolicy","sizes","target","title","type"],u=["id","rel"];return{restrict:"E",scope:{rel:"@",href:"@",id:"@?",charset:"@?",crossorigin:"@?",hreflang:"@?",integrity:"@?",media:"@?",methods:"@?",referrerpolicy:"@?",sizes:"@?",target:"@?",title:"@?",type:"@?"},link:function(t){var n="link"+r(t,u);i.filter(function(e){return t[e]}).forEach(function(r){t.$watch(r,function(i){"undefined"!=typeof i&&e.update(n,r,t[r])})})}}}t.$inject=["$log","updateAttribute"],angular.module("updateMeta").directive("updateLink",t)}(),function(){function t(t,e){return{restrict:"E",scope:{charset:"@",name:"@",content:"@",httpEquiv:"@",scheme:"@",property:"@",itemprop:"@"},link:function(t){var n;t.name&&(n='meta[name="'+t.name+'"]'),t.httpEquiv&&(n='meta[http-equiv="'+t.httpEquiv+'"]'),t.property&&(n='meta[property="'+t.property+'"]'),t.itemprop&&(n='meta[itemprop="'+t.itemprop+'"]'),t.$watch("content",function(r){"undefined"!=typeof r&&e.update(n,"content",t.content)}),t.$watch("charset",function(n){"undefined"!=typeof n&&e.update("meta[charset]","charset",t.charset)})}}}t.$inject=["$log","updateAttribute"],angular.module("updateMeta").directive("updateMeta",t)}(),function(){function t(t,e){return{restrict:"E",scope:{type:"@",content:"="},link:function(t){var n='script[type="'+t.type+'"]';t.$watch("content",function(r){"undefined"!=typeof r&&e.update(n,t.content)})}}}t.$inject=["$log","updateScriptContent"],angular.module("updateMeta").directive("updateScript",t)}(),function(){function t(){return{restrict:"E",scope:{title:"@"},link:function(t){t.$watch("title",function(t){"undefined"!=typeof t&&document&&(document.title=t)})}}}t.$inject=["$log"],angular.module("updateMeta").directive("updateTitle",t)}(),function(){function t(t){function e(e,n,r){if(!document)return t.error("updateMeta: document is not available!"),void 0;if(!e)return t.error("updateMeta: please provide a selector"),void 0;var i=document.querySelector(e);i&&i.setAttribute&&i.setAttribute(n,r)}return{update:e}}t.$inject=["$log"],angular.module("updateMeta").factory("updateAttribute",t)}(),function(){function t(t,e,n){return{update:function(r,i){if(!document)return t.error("updateMeta: document is not available!"),void 0;if(!r)return t.error("updateMeta: please provide a selector"),void 0;var u=document.querySelector(r);if(u){var o=e.trustAsHtml(n("json")(i));u.outerHTML='<script type="application/ld+json">'+o+"</script>"}}}}t.$inject=["$log","$sce","$filter"],angular.module("updateMeta").factory("updateScriptContent",t)}(); |
{ | ||
"name": "angular-update-meta", | ||
"version": "2.0.1", | ||
"version": "2.1.0", | ||
"main": "dist/update-meta.js", | ||
@@ -5,0 +5,0 @@ "author": { |
@@ -44,2 +44,3 @@ # Update meta tags in AngularJS | ||
<meta itemprop="description" content="Application wide description for Schema.org (Google+ uses this)"> | ||
<script type="application/ld+json"></script> | ||
</head> | ||
@@ -61,4 +62,19 @@ <body ng-app="yourApp"> | ||
<update-meta itemprop="description" content="A page specific itemprop description"></update-meta> | ||
<update-script content="structuredData" type="application/ld+json"></update-script> | ||
``` | ||
And if you want to use [Google Structured Data](https://developers.google.com/search/docs/guides/intro-structured-data), create an object like this in $scope: | ||
```javascript | ||
$scope.structuredData = { | ||
"@context": "http://schema.org", | ||
"@type": "Organization", | ||
"url": "http://www.example.com", | ||
"name": "Unlimited Ball Bearings Corp.", | ||
"contactPoint": { | ||
"@type": "ContactPoint", | ||
"telephone": "+1-401-555-1212", | ||
"contactType": "Customer service" | ||
} | ||
}; | ||
``` | ||
So the head is updated to: | ||
@@ -75,2 +91,15 @@ | ||
<meta itemprop="description" content="A page specific itemprop description"> | ||
<script type="application/ld+json"> | ||
{ | ||
"@context": "http://schema.org", | ||
"@type": "Organization", | ||
"url": "http://www.example.com", | ||
"name": "Unlimited Ball Bearings Corp.", | ||
"contactPoint": { | ||
"@type": "ContactPoint", | ||
"telephone": "+1-401-555-1212", | ||
"contactType": "Customer service" | ||
} | ||
} | ||
</script> | ||
</head> | ||
@@ -91,3 +120,3 @@ <body ng-app="yourApp"> | ||
You can also update link tags. If you have the following markup in your template: | ||
You can also update link tags. If you have the following markup in your layout: | ||
@@ -124,3 +153,3 @@ ```xml | ||
A `rel` and a `href` attribute must be supplied to the directive. The directive will try querying the DOM using the `rel` and `href` attributes, and optionally an `id`. Any of the supported attributes (`id`, `charset`, `crossorigin`, `hreflang`, `integrity`, `media`, `methods`, `referrerpolicy`, `sizes`, `target`, `title`, `type`) wil be updated. | ||
A `rel` and a `href` attribute must be supplied to the directive. The directive will try querying the DOM using the `rel` and `href` attributes, and optionally an `id`. Any of the supported attributes (`id`, `charset`, `crossorigin`, `hreflang`, `integrity`, `media`, `methods`, `referrerpolicy`, `sizes`, `target`, `title`, `type`) will be updated. | ||
@@ -161,2 +190,6 @@ ### Expressions | ||
### 2.1.0 | ||
- added support for Google Structured Data (credits to [Mostafa Shaheydari](https://github.com/Pymossy)) | ||
### 2.0.1 | ||
@@ -181,3 +214,3 @@ | ||
- fix bower dependencies | ||
- fixed bower dependencies | ||
@@ -184,0 +217,0 @@ ### v1.7.0 |
41890
27
736
262