Sibeesh Passion

Top Menu

  • Home
  • Search
  • About
  • Privacy Policy

Main Menu

  • Articles
    • Azure
    • .NET
    • IoT
    • JavaScript
    • Career Advice
    • Interview
    • Angular
    • Node JS
    • JQuery
    • Knockout JS
    • Jasmine Framework
    • SQL
    • MongoDB
    • MySQL
    • WordPress
  • Contributions
    • Medium
    • GitHub
    • Stack Overflow
    • Unsplash
    • ASP.NET Forum
    • C# Corner
    • Code Project
    • DZone
    • MSDN
  • Social Media
    • LinkedIn
    • Facebook
    • Instagram
    • Twitter
  • YouTube
    • Sibeesh Venu
    • Sibeesh Passion
  • Awards
  • Home
  • Search
  • About
  • Privacy Policy

logo

Sibeesh Passion

  • Articles
    • Azure
    • .NET
    • IoT
    • JavaScript
    • Career Advice
    • Interview
    • Angular
    • Node JS
    • JQuery
    • Knockout JS
    • Jasmine Framework
    • SQL
    • MongoDB
    • MySQL
    • WordPress
  • Contributions
    • Medium
    • GitHub
    • Stack Overflow
    • Unsplash
    • ASP.NET Forum
    • C# Corner
    • Code Project
    • DZone
    • MSDN
  • Social Media
    • LinkedIn
    • Facebook
    • Instagram
    • Twitter
  • YouTube
    • Sibeesh Venu
    • Sibeesh Passion
  • Awards
  • Linux Azure Function Isolated Dot Net 9 YAML Template Deployment

  • Build, Deploy, Configure CI &CD Your Static Website in 5 mins

  • Post Messages to Microsoft Teams Using Python

  • Get Azure Blob Storage Blob Metadata Using PowerShell

  • Deploy .net 6 App to Azure from Azure DevOps using Pipelines

Angular
Home›Angular›Change The Page Title Dynamically Using Angular JS

Change The Page Title Dynamically Using Angular JS

By SibeeshVenu
March 7, 2016
3182
0
Share:
Empty Website In Visual Studio

In this article we are going to see how we can change the title of a page dynamically using Angular JS. We will be showing random titles which we have already set in an array to the user whenever user reload the same page. To implement this, we are creating angular js controller and service. And we are treating the html tag of our page as our angular js app module and controller. Now shall we go and see this in detail? I hope you will like this.

Download the source code

You can always download the source code here:

  • Change The Page Title Dynamically
  • Background

    For the past few days I am just doing some experiments with Angular JS. If you want to see my latest articles related to Angular JS, Please see here: Angular JS Latest Articles . Here in this post we are going to change the page title dynamically in each user actions. I hope you all know how much important a title tag is in a page. Let us see that first.

    Importance Of Title Tag

  • A title tag add title in browser toolbar
  • It also provides title for the pages when the page is added to favorites
  • Positively affect your page ranking in Google search, by displaying title for the page in search result
  • Now we will start our coding. I hope you will enjoy reading.

    Create an Empty Website in Visual Studio

    Click File-> New-> Web Site.

    Empty Website In Visual Studio

    Empty Website In Visual Studio

    Install Angular JS from NuGet Packages

    Once your application is opened, please install Angular JS first, since we are going to do all of our coding part using Angular JS.

    Install Angular JS From NuGet Packages

    Install Angular JS From NuGet Packages

    Now create a new page, start coding.

    Using The Code

    Before starting, we need to add the needed references to our page right?

    [html]
    <script src="Scripts/angular.min.js"></script>
    <script src="Scripts/angular-aria.min.js"></script>
    <script src="Scripts/angular-route.min.js"></script>
    <script src="Scripts/myScripts.js"></script>
    [/html]

    Here myScripts.js”/em> is our JavaScript file where we are going to write out Angular JS scripts. Once you add the reference we will make some changes in our page as follows.

    [html]
    <!DOCTYPE html>
    <html ng-app="titApp" ng-controller="titCtrl as t">
    <head>
    <title>{{t.title}} – Sibeesh Passion</title>

    <meta charset="utf-8" />
    <script src="Scripts/angular.min.js"></script>
    <script src="Scripts/angular-aria.min.js"></script>
    <script src="Scripts/angular-route.min.js"></script>
    <script src="Scripts/myScripts.js"></script>
    </head>
    <body>
    <h1>{{t.title}}</h1>
    </body>
    </html>

    [/html]

    As you can see titApp is our Angular JS app name and titCtrl is our controller name, now we will start writing the scripts. Are you ready?

    Add Angular JS App

    You can add an angular js app as follows.

    [js]
    (function () {
    var app;
    app = angular.module(‘titApp’, []);
    })();
    [/js]

    Now we will create our controller.

    Add Angular JS Controller

    Below is our Angular JS controller scripts.

    [js]
    app.controller(‘titCtrl’, function ($scope, myFactory) {
    var num = Math.floor(Math.random() * 6) + 1;
    var newTit = [‘Change Page Layout Dynamically Using jQuery Layout Plug in’, ‘February 2016 Month Winner In C-Sharp Corner’,
    ‘Custom Deferred Grid Using MVC Web API And Angular JS’, ‘TagIt Control With Data From Database Using Angular JS In MVC Web API’,
    ‘jQuery Datatable With Server Side Data’, ‘Programmatically Extract or Unzip Zip,Rar Files And Check’];
    myFactory.setTitle(newTit[num]);
    var tt = myFactory.getTitle();
    if (tt != undefined) {
    this.title = tt;
    } else {
    console.log(‘Oops! Something went wrong while fetching the data.’);
    }
    });
    [/js]

    Here myFactory is our Angular JS service name, and as you can see we have set an array with possible tiles in it already. We are generating one random number between 1 to 6 and take the appropriate value from the array by index. You can always load these data from a database instead. Here we uses two functions setTitle and getTitle, to set the title and get the title. Now we will see our Angular JS service scripts.

    Add Angular JS Service

    [js]
    app.service(‘myFactory’, function () {
    var varTitle = ‘Change Title Dynamically Demo’;
    this.getTitle = function () {
    return varTitle;
    };
    this.setTitle = function (tit) {
    varTitle = tit;
    };
    });
    [/js]

    Now let us see the complete Angular JS scripts.

    Complete Scripts

    [js]
    (function () {
    var app;
    app = angular.module(‘titApp’, []);
    app.controller(‘titCtrl’, function ($scope, myFactory) {
    var num = Math.floor(Math.random() * 6) + 1;
    var newTit = [‘Change Page Layout Dynamically Using jQuery Layout Plug in’, ‘February 2016 Month Winner In C-Sharp Corner’,
    ‘Custom Deferred Grid Using MVC Web API And Angular JS’, ‘TagIt Control With Data From Database Using Angular JS In MVC Web API’,
    ‘jQuery Datatable With Server Side Data’, ‘Programmatically Extract or Unzip Zip,Rar Files And Check’];
    myFactory.setTitle(newTit[num]);
    var tt = myFactory.getTitle();
    if (tt != undefined) {
    this.title = tt;
    } else {
    console.log(‘Oops! Something went wrong while fetching the data.’);
    }
    });
    app.service(‘myFactory’, function () {
    var varTitle = ‘Change Title Dynamically Demo’;
    this.getTitle = function () {
    return varTitle;
    };
    this.setTitle = function (tit) {
    varTitle = tit;
    };
    });
    })();
    [/js]

    We have done everything needed, Now it is time to see the output.

    Output

    Chnage_Page_Title_Dynamically_Using_Angular_JS_Output

    Chnage_Page_Title_Dynamically_Using_Angular_JS_Output

    Chnage_Page_Title_Dynamically_Using_Angular_JS_Output

    Chnage_Page_Title_Dynamically_Using_Angular_JS_Output

    Have a happy coding.

    Conclusion

    Did I miss anything that you may think which is needed? Have you ever wanted to do this requirement? Could you find this post as useful? I hope you liked this article. Please share me your valuable suggestions and feedback.

    Your turn. What do you think?

    A blog isn’t a blog without comments, but do try to stay on topic. If you have a question unrelated to this post, you’re better off posting it on C# Corner, Code Project, Stack Overflow, Asp.Net Forum instead of commenting here. Tweet or email me a link to your question there and I’ll definitely try to help if I can.

    Kindest Regards
    Sibeesh Venu

    TagsAngular JSAngular JS ServiceChange The Page Title Dynamically
    Previous Article

    February 2016 Month Winner In C-Sharp Corner

    Next Article

    Programmatically Change The Element Or Page Into ...

    0
    Shares
    • 0
    • +
    • 0
    • 0
    • 0

    SibeeshVenu

    I am Sibeesh Venu, an engineer by profession and writer by passion. Microsoft MVP, Author, Speaker, Content Creator, Youtuber, Programmer.

    Related articles More from author

    • Angular

      AngularJS: When/Where to Load Files

      June 17, 2015
      By SibeeshVenu
    • Insertion_In_Table
      .NETAngularSQLWeb API

      TagIt Control With Data From Database Using Angular JS In MVC Web API

      February 25, 2016
      By SibeeshVenu
    • Chart Widgets With Server Side Data In MVC Using Angular JS And Web API Output
      .NETAngularASP.NETWeb API

      Chart Widgets With Server Side Data In MVC Using Angular JS And Web API

      March 17, 2016
      By SibeeshVenu
    • Learning AngularJS
      Angular

      Learning AngularJS: HTML DOM

      April 29, 2015
      By SibeeshVenu
    • Dynamic Angular JS Tabs In MVC Figure 1
      Angular

      How To Create Dynamic Angular JS Tabs In MVC

      February 16, 2016
      By SibeeshVenu
    • Convert XML To JSON In Angular JS
      Angular

      Convert XML to JSON In Angular JS

      November 3, 2015
      By SibeeshVenu
    0

    My book

    Asp Net Core and Azure with Raspberry Pi Sibeesh Venu

    YouTube

    MICROSOFT MVP (2016-2022)

    profile for Sibeesh Venu - Microsoft MVP

    Recent Posts

    • Linux Azure Function Isolated Dot Net 9 YAML Template Deployment
    • Build, Deploy, Configure CI &CD Your Static Website in 5 mins
    • Easily move data from one COSMOS DB to another
    • .NET 8 New and Efficient Way to Check IP is in Given IP Range
    • Async Client IP safelist for Dot NET
    • Post Messages to Microsoft Teams Using Python
    • Get Azure Blob Storage Blob Metadata Using PowerShell
    • Deploy .net 6 App to Azure from Azure DevOps using Pipelines
    • Integrate Azure App Insights in 1 Minute to .Net6 Application
    • Azure DevOps Service Connection with Multiple Azure Resource Group

    Tags

    Achievements (35) Angular (14) Angular 5 (7) Angular JS (15) article (10) Article Of The Day (13) Asp.Net (14) Azure (65) Azure DevOps (10) Azure Function (10) Azure IoT (7) C# (17) c-sharp corner (13) Career Advice (11) chart (11) CSharp (7) CSS (7) CSS3 (6) HighChart (10) How To (9) HTML5 (10) HTML5 Chart (11) Interview (6) IoT (11) Javascript (10) JQuery (82) jquery functions (9) JQWidgets (15) JQX Grid (17) Json (7) Microsoft (8) MVC (20) MVP (9) MXChip (7) News (18) Office 365 (7) Products (10) SQL (20) SQL Server (15) Visual Studio (10) Visual Studio 2017 (7) VS2017 (7) Web API (12) Windows 10 (7) Wordpress (9)
    • .NET
    • Achievements
    • ADO.NET
    • Android
    • Angular
    • Arduino
    • Article Of The Day
    • ASP.NET
    • Asp.Net Core
    • Automobile
    • Awards
    • Azure
    • Azure CDN
    • azure devops
    • Blockchain
    • Blog
    • Browser
    • C-Sharp Corner
    • C#
    • Career Advice
    • Code Snippets
    • CodeProject
    • Cognitive Services
    • Cosmos DB
    • CSS
    • CSS3
    • Data Factory
    • Database
    • Docker
    • Drawings
    • Drill Down Chart
    • English
    • Excel Programming
    • Exporting
    • Facebook
    • Fun
    • Gadgets
    • GitHub
    • GoPro
    • High Map
    • HighChart
    • How to
    • HTML
    • HTML5
    • Ignite UI
    • IIS
    • Interview
    • IoT
    • JavaScript
    • JQuery
    • jQuery UI
    • JQWidgets
    • JQX Grid
    • Json
    • Knockout JS
    • Linux
    • Machine Learning
    • Malayalam
    • Malayalam Poems
    • MDX Query
    • Microsoft
    • Microsoft ADOMD
    • Microsoft MVP
    • Microsoft Office
    • Microsoft Technologies
    • Microsoft Windows
    • Microsoft Windows Server
    • Mobile
    • MongoDB
    • Monthly Winners
    • MVC
    • MVC Grid
    • MySQL
    • News
    • Node JS
    • npm
    • Number Conversions
    • October 2015
    • Office 365
    • Office Development
    • One Plus
    • Outlook
    • Page
    • PHP
    • Poems
    • PowerShell
    • Products
    • Q&A
    • Raspberry PI
    • React
    • SEO
    • SharePoint
    • Skype
    • Social Media
    • Software
    • Spire.Doc
    • Spire.PDF
    • Spire.XLS
    • SQL
    • SQL Server
    • SSAS
    • SSMS
    • Storage In HTML5
    • Stories
    • Third Party Software Apps
    • Tips
    • Tools
    • Translator Text
    • Uncategorized
    • Unit Testing
    • UWP
    • VB.Net
    • Videos
    • Virtual Machine
    • Visual Studio
    • Visual Studio 2017
    • Wamp Server
    • Web API
    • Web Platform Installer
    • Webinars
    • WebMatrix
    • Windows 10
    • Windows 7
    • Windows 8.1
    • Wordpress
    • Writing

    ABOUT ME

    I am Sibeesh Venu, an engineer by profession and writer by passion. Microsoft MVP, Author, Speaker, Content Creator, Youtuber, Programmer. If you would like to know more about me, you can read my story here.

    Contact Me

    • info@sibeeshpassion.com

    Pages

    • About
    • Search
    • Privacy Policy
    • About
    • Search
    • Privacy Policy
    © Copyright Sibeesh Passion 2014-2025. All Rights Reserved.
    Go to mobile version