Change The Page Title Dynamically Using AngularJS

We will be showing random titles which we have already set in an array to the user whenever the user reloads the same page. To implement this, we are creating an 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.

Background

For the past few days I have been 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 action. 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 adds a title in browser toolbar.
  • It also provides a title for the pages when the page is added to favorites.
  • Positively affects 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?

  1. <script src="Scripts/angular.min.js"></script>  
  2. <script src="Scripts/angular-aria.min.js"></script>  
  3. <script src="Scripts/angular-route.min.js"></script>  
  4. <script src="Scripts/myScripts.js"></script>  
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.
  1. <em><!DOCTYPE html>  
  2. <html ng-app="titApp" ng-controller="titCtrl as t">  
  3. <head>  
  4. <title>{{t.title}} - Sibeesh Passion</title>  
  5. <meta charset="utf-8" />  
  6. <script src="Scripts/angular.min.js"></script>  
  7. <script src="Scripts/angular-aria.min.js"></script>  
  8. <script src="Scripts/angular-route.min.js"></script>  
  9. <script src="Scripts/myScripts.js"></script>  
  10. </head>  
  11. <body>  
  12. <h1>{{t.title}}</h1>  
  13. </body>  
  14. </html>  
  15. </em>  
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.

  1. <em>(function ()  
  2. {  
  3. var app;  
  4. app = angular.module('titApp', []);  
  5. })();  
  6. </em>  
Now we will create our controller.

 

Add Angular JS Controller

Below is our Angular JS controller scripts.

  1. <em>app.controller('titCtrl', function ($scope, myFactory)  
  2. {  
  3. var num = Math.floor(Math.random() * 6) + 1;  
  4. var newTit = ['Change Page Layout Dynamically Using jQuery Layout Plug in''February 2016 Month Winner In C-Sharp Corner',  
  5. 'Custom Deferred Grid Using MVC Web API And Angular JS''TagIt Control With Data From Database Using Angular JS In MVC Web API',  
  6. 'jQuery Datatable With Server Side Data''Programmatically Extract or Unzip Zip,Rar Files And Check'];  
  7. myFactory.setTitle(newTit[num]);  
  8. var tt = myFactory.getTitle();  
  9. if (tt != undefined)  
  10. {  
  11. this.title = tt;  
  12. else  
  13. {  
  14. console.log('Oops! Something went wrong while fetching the data.');  
  15. }  
  16. });  
  17. </em>  
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 use two functions setTitle andgetTitle, to set the title and get the title. Now we will see our Angular JS service scripts.

 

Add Angular JS Service

  1. <em>app.service('myFactory', function ()  
  2. {  
  3. var varTitle = 'Change Title Dynamically Demo';  
  4. this.getTitle = function ()  
  5. {  
  6. return varTitle;  
  7. };  
  8. this.setTitle = function (tit)  
  9. {  
  10. varTitle = tit;  
  11. };  
  12. });  
  13. </em>  

 

Now let us see the complete Angular JS scripts.

Complete Scripts

  1. <em>(function ()  
  2. {  
  3. var app;  
  4. app = angular.module('titApp', []);  
  5. app.controller('titCtrl', function ($scope, myFactory)  
  6. {  
  7. var num = Math.floor(Math.random() * 6) + 1;  
  8. var newTit = ['Change Page Layout Dynamically Using jQuery Layout Plug in''February 2016 Month Winner In C-Sharp Corner',  
  9. 'Custom Deferred Grid Using MVC Web API And Angular JS''TagIt Control With Data From Database Using Angular JS In MVC Web API',  
  10. 'jQuery Datatable With Server Side Data''Programmatically Extract or Unzip Zip,Rar Files And Check'];  
  11. myFactory.setTitle(newTit[num]);  
  12. var tt = myFactory.getTitle();  
  13. if (tt != undefined)  
  14. {  
  15. this.title = tt;  
  16. else  
  17. {  
  18. console.log('Oops! Something went wrong while fetching the data.');  
  19. }  
  20. });  
  21. app.service('myFactory', function ()  
  22. {  
  23. var varTitle = 'Change Title Dynamically Demo';  
  24. this.getTitle = function ()   
  25. {  
  26. return varTitle;  
  27. };  
  28. this.setTitle = function (tit)  
  29. {  
  30. varTitle = tit;  
  31. };  
  32. });  
  33. })();  
  34. </em>  
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

Happy coding.

Conclusion

Did I miss anything that you may think is needed? Have you ever wanted to do this requirement? Did you find this post useful? I hope you liked this article. Please share with 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.

Please see this article in my blog here.

Next Recommended Readings