Giter Club home page Giter Club logo

angular-metatags's Introduction

Angular MetaTags module

Module to dynamically provide metatags based on the path.

How to use it

####Include angular-metatags.js or angular-metatags.min.js to your html file before your app script and after the angular's core script

<script src="bower_components/angular/angular.js"></script>
<script src="bower_components/angular-route/angular-route.js"></script>
<script src="scripts/angular-metatags.js"></script>
<script src="scripts/mainApp.js"></script>

####Include the module in your app

var myApp = angular.module('myApp', ['ngRoute','metatags']);

####Inject the MetaTagsProvider in the config and define your meta tags

myApp.config(['$routeProvider','MetaTagsProvider', function($routeProvider, MetaTagsProvider) {

        ...
        ...
        
        MetaTagsProvider
          .when('/', {
            title: 'Great',
            description: 'Cool'
            fb_title: 'My title'
            fb_site_name: 'My site name' 
            fb_url: 'www.blablabla.blabla' 
            fb_description: 'Cool website'
            fb_type: 'Facebook type'
            fb_image: 'an_image.jpg' 
          })
          .when('/page1/:parameter1/:parameter2',{
            title: 'Page 1 :something',
            description: function(parameter1, parameter2){
                return 'COOOOOOOL' + parameter1 + " Super " + parameter2;
            }
            robots: 'index, follow'
            keywords: 'some cool keywords'
          })
          .when('/page2/:parameter1',{
            title: 'Page 2 of :parameter1',
            description: 'Another great page'
          })
          .otherwise({
            title: 'otherwise',
            description: 'Another great page'
          })
    }]);

when Accepts a string with the path and an object with metatags when(path,metatags) The path can contain parameters, each parameter should start with : . Each attribute of the metatags object can be a string or a function that returns a string. The string can contain a parameter that will be replaced. If the path contain parameters and an attribute of the metatags object is a function the parameters are passed to that function.

Example

If we define a route like this

.when('/page1/:parameter1/:parameter2',{
    title: 'Books of :parameter1 by :parameter2',
    description: function(parameter1, parameter2){
      return 'We have great books of ' + parameter1.toUpperCase() + ' by the amazing :parameter2';
    },
    robots: 'index, follow',
    keywords: function(parameter1){
      var keywords = ['history', 'art', 'music']
      keywords.push(parameter1);
      return keywords.join(' ');
    }
})

and we visit the path /page1/geography/nationalgeographic We will have the following object of metatags:

$rootScope.metatags =  { 
  title: "Books of geography by nationalgeographic", 
  description: "We have great books of GEOGRAPHY by the amazing nationalgeographic", 
  robots: "index, follow", 
  keywords: "history art music geography" 
}

####Initialize the provider on your application run

myApp.run(function(MetaTags){
    MetaTags.initialize();
});

####Include the metatags in your html

You can use the metatags in our html like this:

  <title>{{ metatags.title }}</title>
  <meta name="description" content="{{ metatags.description }}" >
  <meta name="robots" content="{{ metatags.robots }}" >
  <meta name="keywords" content="{{ metatags.keywords }}" >
  <!-- Facebook related metatags -->
  <meta property="fb:app_id"          content="{{ metatags.fb_app_id }}" > 
  <meta property="og:url"             content="{{ metatags.fb_url }}"  > 
  <meta property="og:title"           content="{{ metatags.fb_title }}" > 
  <meta property="og:image"           content="{{ metatags.fb_image }}"  > 
  <meta property="og:description"     content="{{ metatags.fb_description }}"  >
  <meta property="og:site_name"       content="{{ metatags.fb_site_name }}" >
  <meta property="og:type"            content="{{ metatags.fb_type }}" >

Angular and SEO

Until the search engine bots will be able to execute javascript properly you will have to use a tool like prerender.io or brombone to serve prerendered pages when a bot visit your site. You can read more for the topic on the following articles:

-Weluse.de - Angular & SEO finally a piece of cake

-Builtvisible.com - The Basics of JavaScript Framework SEO in AngularJS

-Yearofmoo.com - AngularJS and SEO

angular-metatags's People

Contributors

avraammavridis avatar daikazu avatar gorodeckiy avatar heavysixer avatar

Watchers

James Cloos avatar Karthikeyan avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.