Guide on incorporating an SVG file according to the user's role within an Angular application

How can I dynamically import an SVG file based on the user's role, whether they are an admin or a regular user?

Currently, my code in layout.ts looks like this:

<div class="layout-body h-100 ">
  <ng-container [ngTemplateOutlet]="svgBuilding"></ng-container>
</div>

<svg version="1.1" id="Layer_1" class="h-100 w-100" xmlns="http://www.w3.org/2000/svg"
    xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 1035.8 534.8"
    style="enable-background:new 0 0 1035.8 534.8;" xml:space="preserve">
    <style type="text/css">
      g.building {
        fill: #FFFFFF;
      }

      g.building:hover {
        fill: #e2e2e2 !important;
        cursor: pointer;
      }
      
      // More styling properties for various elements...

    </style>
    <g id="building_a" class="building" (click)="openBuilding('BLDGA')">
      <path class="st0" d="M306.3,171.4v62.7H166.1v-1.8c-0.7,0-1.7,0.8-2,1.1v0.8H114c0.4,0.9,0.6,1.9-0.1,1.8c-1-0.2-1.3-1.2-1.3-1.8
        h-10.3v-62.7h23.9v-0.1h43.1v0.1h14.4v-7.7h46.7v7.7h75.9V171.4z" />
      <foreignObject x="240" y="170" width="75px" height="50px" style="position:relative;">
        <xhtml:div xmlns="http://www.w3.org/1999/xhtml">
          <div *ngIf="count('BLDGA')">
            <div class="mechanic">
              <nz-badge [nzCount]="mechCount('BLDGA') || 0">
                <img src="./assets/images/room/room.png" width="45px" height="35px" />
              </nz-badge>
            </div>
          </div>
        </xhtml:div>
      </foreignObject>
     .....
    ....
    ...
    ..
    </svg>

I have multiple SVG files and I'm looking to simplify the code in layout.ts by creating a function that dynamically imports the appropriate SVG file based on the user's role.

For example, one SVG is intended for admins only, another for advisors, and another for students.

SVG1 - Admin SVG2 - Advisor SVG3 - Student

Answer №1

To handle different .svg files, we can implement the following approach:

<img [src]="role=='admin'?'assets/svg1.svg':
            role=='advisor'?'assets/svg2.svg':
            'assets/svg3.svg'">

Alternatively, we can create a separate variable like this:

images={
  admin:'assets/svg1.svg',
  advisor:'assets/svg2.svg',
  student:'assets/svg3.svg'
}
//and then use
    <img [src]="images[role]'">

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Utilize tags as properties in Vue.js by selecting them

I am attempting to retrieve a value from a select tag and use it in an object property. Below is the HTML code: <div id="app"> <h3>{{title}}</h3> <div class="form"> <div class="form-group"> <div ...

Module or its corresponding type declarations not found in the specified location.ts(2307)

After creating my own npm package at https://www.npmjs.com/package/leon-theme?activeTab=code, I proceeded to set up a basic create-react-app project at https://github.com/leongaban/test-project. In the src/index.tsx file of my react app, I attempted to im ...

Dispensing guarantees with Protractor

q library offers a unique feature that allows resolving and spreading multiple promises into separate arguments: If you have a promise for an array, you can utilize spread instead of then. The spread function distributes the values as arguments in the f ...

Why is the text returned by the Angular Response body missing the JSON brackets? Strange, isn't it

As a newcomer to Angular 2, I should mention that some information is internal and will be replaced with placeholders when needed. My current task involves making a simple post request and retrieving the contents of the request body. Below is my existing ...

Can React Native support styling using server-side data?

One of my React Native (RN) components is rendering data from an external server. The data is enclosed within RN components. For example: ... <View> <Text>{this.props.db.greeting}</Text> </View> The 'DB' object is si ...

Typescript is unable to access the global variables of Node.js

After using Typescript 1.8 with typings, I made the decision to upgrade to typescript 2.8 with @types. When I downloaded @types/node, my console started showing errors: error TS2304: Cannot find name 'require'. The project structure is as foll ...

What is the best way to extract the content within a nested <p> element from an external HTML document using the HTML Agility Pack?

I'm currently working on retrieving text from an external website that is nested within a paragraph tag. The enclosing div element has been assigned a class value. Take a look at the HTML snippet: <div class="discription"><p>this is the ...

choose among various options in Javascript

I'm currently using PHP and AJAX to create a basic CRUD system. I want to display a form with three buttons: Grabar, Modificar, and Eliminar. The issue I'm facing is determining the action to take based on which button is clicked. For instance, c ...

Mastering the art of completing a form using AJAX

I'm working on a checkbox that triggers AJAX to create a new log. It populates the necessary information and automatically clicks the "create" button. However, I'm facing an issue where the hour value is not changing. Any help on what I might be ...

Vue.js: Synchronization of props may not happen instantly

Struggling with using vue.js to synchronize a prop between parent and child components. The challenge is that sync relies on events, so every time I update the value, I have to wait for $nextTick before seeing the update. It's cumbersome to include $n ...

Comparing strings in AngularJS

Struggling to compare two strings in AngularJS, I've come across various examples online. From what I understand, there are different methods like angular.equals(str1, str2), ===, and == if you're certain both are strings... Despite trying all t ...

Non-responsive image in Bootstrap 4

My struggle is with making my website fully responsive. Whenever I use the img tag with a width of 100% on a large screen, the image fits perfectly, but when the window is resized, a horizontal scroll appears, creating white spaces and causing the image ...

Converting a nested array of children into a string within a multidimensional array

I am currently in the process of updating a single value (ElementStatus) within the multidimensional JSON file shown below. BuildingID: 1521 BuildingName: "PEN LLOYD BUILDING" BuildingNumber: "A" ElementList: Array(15) 0: {ElementID: 114, SurveyTypeID: 3, ...

What could be causing the no-repeat functionality to fail on these background images, and what steps can be taken to prevent the list text from overlapping with the image?

UPDATE EDIT. There is still an unresolved issue with this problem. Although setting the background to no-repeat fixed the image repetition problem, the list item text still overlaps the background image and there is an extra tick to the left. Please refer ...

Nested data is the foundation of the Polymer framework's dom-repeat

Struggling with implementing Polymer's <dom-repeat>. Working with parent objects (folders) and child objects (contents within the folders) that are both generated from AJAX responses. The desired output should look like this: Folder child c ...

``I'm having trouble getting the onchange event to function properly in a customized

After following a tutorial, I successfully created a custom select dropdown using the provided link. https://www.w3schools.com/howto/tryit.asp?filename=tryhow_custom_select However, I am facing an issue with the onchange event not working for the select ...

Breaking content into two sections using Javascript or jQuery

Uncertain if Javascript can handle this task, any assistance or advice is appreciated. Consider this scenario: Suppose I have a 6-paragraph long content (text only) being pulled dynamically from the database all at once. This means that all 6 paragraphs a ...

What is the best way to incorporate a YouTube video into my HTML webpage?

Looking to add some YouTube links to my webpage - I'm a complete newbie and clueless about formats and embedding. Can someone guide me on what to use and how to insert it into my HTML code? Appreciate any help! ...

What could be the reason border-radius doesn't function properly on IE9, while it works fine on IE10 and Chrome?

Encountering an issue with IE9 where the border-radius property is not working in one div, while functioning perfectly in other divs. Browser Comparison: IE9 IE10, Chrome, FF Html Code: <article id="main-login"> <div class="radius-10 s ...

In a local setting, the KendoUI chips are displaying without their borders

I recently tested out the code from kendo on StackBlitz and it worked perfectly: https://i.stack.imgur.com/Nrp2A.png However, when running the same code on my localhost, all the styling for the chip is missing: https://i.stack.imgur.com/1pUH9.png The c ...