Angular Dynamic CSS Loading from Azure Blob Storage

Introduction

 
Welcome back, everyone! It feels good to get motivated by all community readers and by their valuable comments. Therefore, I will share my knowledge continuously by writing articles. In this article, we will explore loading a dynamic CSS from the Azure Blob Storage. Basically, we will learn about Azure Blob storage and a basic knowledge of CSS. Let's take a small look at Azure Blob Storage.
 
Azure Blob Storage is a cloud service that acts like an object store, where we can use it to save the data objects such as images, video files, etc. Now let’s wrap this thing up and let’s get back to the hands-on.
 
Prerequisites:
 
An Active Azure Login Subscription
 
Step 1
 
Let’s create a CSS file and upload the CSS file into an Azure Blob Storage. For uploading the CSS file to blob storage, we can use Microsoft Azure Storage Explorer. If you don’t have it, no hard feelings, you can download it from the following link: Azure Storage.
 
After downloading and installing, we need to create a connection. Go-to >> Storage Account menu and Click >> Connect to Azure Storage options and it will open in a new window.
 
Step 2
 
We can use any way of establishing the connection with the Azure Blob storage. Anyways I have selected >> Using a connection string and Click >> Next
 
Angular Dynamic CSS Loading From Azure Blob Storage
 
Step 3
 
Continue to the next step, just copy and paste the storage connection URL and connect to the blob storage. Once the connection URL is valid, the display name will be automatically bound, then Click >> Connect.
 
Angular Dynamic CSS Loading From Azure Blob Storage
 
Step 3
 
After the successful establishment of connection with Blob Storage, let’s create Blob Containers. For that Click Create Blob Containers and provide a name for Blob Storage. Here I have provided the name 'Angularcss.'
 
Step 4
 
Now open Angularcss in a container and upload three different style sheets. For your reference, refer to the below image as mentioned, then create a CSS file named blue.css, black.css under the Angular assets folder. Then the upload will be successfully uploaded to the blob storage.
 
Angular Dynamic CSS Loading From Azure Blob Storage
 
Step 5
 
Now let’s continue to the next step. Select >> the CSS file and right-click >> Go to the properties option and get the path of the URL for the CSS files like the sample provided from the following links I have provided below:
  • https://abcd.blob.core.windows.net/angularcss/black.css
  • https://abcd.blob.core.windows.net/angularcss/blue.css
  • https://abcd.blob.core.windows.net/angularcss/green.css
I will explain about this, but here just copy the following code and paste into the app.component.html and app.component.css file for the initial time for the view of change
Copy and paste the code to the app.component.html file
  1. <h3 class="textcolor">Angular dynamic CSS loading from Azure blob storage </h3>  
And now copy and paste the following code inside of the app.component.css file
  1. .textcolor {  
  2.     color: red;  
  3.     text - align: center;  
  4. }  
Run ng serve and open the default color that we set
 
Angular Dynamic CSS Loading From Azure Blob Storage
Step 6
 
In addition to that, we need to load the CSS file from the blob storage, before that let’s take an overlook on DomSanitizer is and bypass SecurityTrustResouceURL
DomSanitizer helps in preventing Cross-Site Scripting Security bugs (XSS) by sanitizing the values to be safe in different DOM contexts.
 
Now to bypass SecurityTrustResourceURL, it provides the bypass security and trust for the given value to be in a safe resource URL meaning the location that may be used to load the executable code like <script src>.
 
Let’s create three different <div> and add (click) event to that <div> and one call function CSSClick() on the click of the <div> and paste the parameter value for identifying which CSS file we have to load.
 
Copy and paste the following code inside of the HTML
  1. <div (click)="CSSClick(1)">Dynamic CSS Blue</div>  
  2. <div (click)="CSSClick(2)">Dynamic CSS Black</div>  
  3. <div (click)="CSSClick(3)">Dynamic CSS Green</div>  
  4. <h3 class="textcolor"> Angular dynamic CSS loading from Azure blob storage </h3>  
An important point in here is the IsDyanmic flag is to maintain the change reflection to the application, since we have initially loaded the default CSS.
  1. import {  
  2.     Component  
  3. } from '@angular/core';  
  4. import {  
  5.     DomSanitizer  
  6. } from '@angular/platform-browser';  
  7. @Component({  
  8.     selector: 'app-root',  
  9.     templateUrl: './app.component.html',  
  10.     styleUrls: ['./app.component.css']  
  11. })  
  12. export class AppComponent {  
  13.     title = 'dynamicassets';  
  14.     IsDynamic = false;  
  15.     URL: string;  
  16.     constructor(public sanitizer: DomSanitizer) {}  
  17.     ngOnInit() {}  
  18.     CSSClick(data) {  
  19.         this.IsDynamic = true;  
  20.         switch (data) {  
  21.             case 1:  
  22.                 this.URL = "https://abced.blob.core.windows.net/angularcss/blue.css";  
  23.                 break;  
  24.             case 2:  
  25.                 this.URL = "https://abced.blob.core.windows.net/angularcss/black.css";  
  26.                 break;  
  27.             case 3:  
  28.                 this.URL = "https://abced.blob.core.windows.net/angularcss/green.css";  
  29.                 break;  
  30.         }  
  31.     }  
  32. }  
Afterwards, click to change the style to load the CSS url from the DomSanitizer.
  1. <link *ngIf="IsDynamic" type="text/css" rel="stylesheet" [href]='sanitizer.bypassSecurityTrustResourceUrl(URL)'>  
Now we can change the color selection on whichever CSS we need to change dynamically. Here, we can see the changes in the assets folder.
 
Angular Dynamic CSS Loading From Azure Blob Storage
Angular Dynamic CSS Loading From Azure Blob Storage
Angular Dynamic CSS Loading From Azure Blob Storage
Now we can see the details are being fetched in the browser. Right Click >> go to inspect the element and Select >> network tab and see the changes here.
 
Angular Dynamic CSS Loading From Azure Blob Storage
 
Angular Dynamic CSS Loading From Azure Blob Storage
 

Conclusion 

 
Finally, it’s time to wrap up. Now we can see how the Angular application is dynamically loaded with CSS. I hope that this article is useful to you. Comment below if you have any queries, and thanks for reading!


Similar Articles